php coding from a design view
Wednesday 30th May 2007 11:29 PM
I've noticed an interesting trend with PHP Developers who started out from a design background. Actually, this applies to all programming languages almost, but since I'm a PHP Developer, I'll use this as my example!
It is a little hard to explain, but I'll give it a shot.
Designers tend to be picky with "layout", if you show a designer something that is a few pixels out of alignment, they will let you know, and go out of their way to fix it. Similarly, aligning something like a logo to have the perfect spacing and breathing room takes a keen eye, and often is a case of nudging items several pixels at a time to get the "perfect position". This is a little like OCD, but trust me, all good designers will be fairly obsessive about this sort of thing!
This translates across into coding, and it is rare, but there are -some- lucky people who transition from design to coding and the difference in code neatness and layout is obvious.
For example, a line of code which is not tabbed correctly inline with everything else would drive a designer insane, while it probably wouldn't bother a regular coder so much as to go out of their way to fix it. This is a simple example, but it extends much further.
The same applies to things like braces, designers are always told to make things readable, and nothing makes things more readable than spacing them out. One brace per line makes the contents of the braces a lot more defined and allows you to "match" braces by reading straight down the page. The same goes with coding practices like comments.
Many a comment has been crammed right in the middle of a block of code or put somewhere that is not obvious. Designers are instilled with the rules of readabilty; that is, in the western world, we read/write from top to bottom and left to right, if a comment is hanging way out in an obscure place, it's harder to notice and likely to be missed. Aligning comments to the left and giving them some nice spacing at the bottom and top makes them very easy to read, and especially when scanning a page while rapidly scrolling (and reading from the left hand side).
Example 1:
Something like this, might "work", but it can be made to look a lot neater (and easier to read, and thus easier to maintain).
for($i=1; $i<10; $i++) { // loop through $i
echo $i;
echo $i+1; // get $i plus 1
if($i > 5) {
echo 'i is greater than 5!'; // alert if i is more than 5
}
}
Example 2:
This code block is physically longer, but in today's age of coding, you shouldn't need to really worry about extra whitespace being a problem, it is now a lot more readable and far easier to locate comments. The tabbing is consistent and each line is distinct from the others.
// loop through $i
for($i=1; $i<10; $i++)
{
echo $i;
// get $i plus 1
echo $i+1;
// alert if i is more than 5
if($i > 5)
{
echo 'i is greater than 5!';
}
}
Comments on this article:
Add Comment: