Standards

I’m sure everyone has heard the phrase, “The great thing about standards is that there are so many to choose from.” I think we finally have a coding standard for everything we do at Found Line – from PHP to CSS to web file names. We’ve gone through a couple of iterations and various options but here’s what we’ve come up with.

For application code we’ve decided to simply follow the Zend Framework PHP Coding Standard. It gives us guidance on PHP file formatting, naming conventions, coding style, and inline documentation. If it works for a project like Zend Framework with a large number of developers it should work for us. Plus, I wouldn’t be surprise if many applications start standardizing on Zend Framework conventions so following their standards should put us in a good position when doing integration work.

For id and class names in XHTML and CSS we’ve decided to adopt the microformats naming principle of dash-separated-lowercase-words. This is the only CSS naming convention I’ve seen that has a real purpose behind it. We will try to use names from existing microformat standards before coming up with our own. However, when we have to come up with our own names at least they will be consistent with the microformat names.

For files that will be web-accessible we’ve decided to use the dash-separated-lowercase-words standard as well. This is for a couple of reasons. First, it’s consistent with what Zend Framework expects. Second, Matt Cutts recommends dashes instead of underscores as word separators.

PHP Users Group Meeting Last Night

The Burlington, VT PHP Users Group met last night. Thank you Bluehouse Group for hosting and providing pizza and soda! Rob Riggen gave a talk on PHP Frameworks: 3 different ones and we went around the room talking about a PHP function or tip that helped us in the past. I decided to talk about the following very simple tip.

How many times have you done something like this:

if ($foo = 1) { }

when you meant to do this:

if ($foo == 1) { }

This is a common mistake to see. A developer accidentally uses the single equal assignment operator instead of the double equals comparison operator. Since PHP will successfully assign the value it will always return true which was not the intended result. A simple way to catch this mistake is to reverse the variable and the literal value:

if (1 == $foo) { }

This still has the intended effect of comparing the two values but if you accidentally use a single equal sign:

if (1 = $foo) { }

then you will get a parse error. Here we’ve introduced the concept of fail-fast into our code. We’d rather get a fatal error letting us know we made a typo then a potentially insidious and hard-to-find logic error.

PHP Users Group Meeting Tonight

The Burlington, VT PHP Users Group will be meeting tonight at 6:00 pm in Richmond, VT. The theme for this meeting is, “My Favorite Function.” Come prepared to talk briefly about a PHP function (or tip) that has helped you out during your PHP development. I still haven’t decided what my function or tip will be. Maybe it will be something really simple like the list() function which can save you a bit of typing or something more complicated like the Iterator interface.

Zend_Layout and Zend_View Enhancements Webinar

I missed the Zend_Layout and Zend_View Enhancements Webinar but the recorded Webinar and slides are now available. The Webinar covers a few of the cool new features that became available in the Zend Framework 1.5 release last week. These new features give web developers who are using Zend Framework a way to solve the problem of maintaining a consistent look-and-feel across multiple web pages. Solutions to this problem I’ve used in the past include:

  • Dreamweaver templates
  • PHP or Smarty includes

The problem with Dreamweaver templates is that a template change requires you to “burn” the change into all of the web pages that use the template. If you have lots of pages this process can get cumbersome or you may forget to upload all of the modified pages. Also, the developer in me cringes at the fact that we repeat the same (X)HTML code across multiple pages – even if Dreamweaver is handling this repetition for me. This just doesn’t feel very DRY (Don’t Repeat Yourself) to me.

PHP or Smarty includes help a lot with the DRY problem. However, you are now leaving it up to each script to decide where the page elements get included. You may accidentally include the header, for example, in the wrong place in one or more pages. Also, if you need to re-arrange the overall page structure you’re stuck changing every single page.

Enter Zend_Layout and the Zend_View enhancements. Zend_View is an implementation of the two-step-vew design pattern and the Zend_View enhancements are a collection of view helpers that are useful alongside Zend_Layout. Check out the Webinar and read Matthew Weier O’Phinney’s article on Using Zend_View Placeholders to Your Advantage.

Plain Old Semantic HTML (POSH)

I didn’t realize someone had coined a clever phrase for this concept. The basic concept of POSH is creating (X)HTML that is semantic, or structural, rather than presentational. Most people experience the web as a visual medium and thus assume that it is a visual medium. Many web development tools (such as Dreamweaver’s WYSIWYG editor) reinforce this misconception. The web is in fact a structured medium and one facade to that structure is its visual side that most of us experience every day. People without the gift of sight, search engine robots, and other web crawlers don’t experience the web visually. In order to make the web available to the widest audience possible we need to build websites with proper structure first. Then we can add presentation via CSS and behavior through a JavaScript library such as jQuery.

Here are the basic steps to make your next website POSH:

  • validate your (X)HTML – invalid markup is not very POSH at all;
  • use tables only for tabular data, never for layout;
  • use existing elements and attributes if they fit the semantics of what you’re creating (e.g. ul, li, dl, dt, dd) instead of generic div or span elements;
  • use class and id names that are semantic, not presentational (e.g. MainNav is semantic but LeftNav implies presentation and is not so POSH);
  • use as little (X)HTML as possible to markup your content.

For those who claim that this is too limiting when it comes to design I suggest you check out css Zen Garden. Very POSH, very cool. Every single design you can pick from uses the same structured XHTML page – the only thing that changes is the CSS style sheet.

Earlier I mentioned something about adding behavior with JavaScript. My favorite JavaScript library is jQuery. jQuery lets you unobtrusively add JavaScript behavior to your (X)HTML pages using simple CSS selectors. This means no inline JavaScript code in your (X)HTML – no onclick or onmouseover event handlers cluttering things up.

POSH + CSS + jQuery (or another unobtrusive JavaScript library) is very cool indeed. But it’s more than just cool. It makes your websites easier to maintain and it opens the door to concepts such as microformats – data that is both human and machine-readable. Are you ready to get POSH?