Space

Sometimes the space around something can assist in the understanding of something as much as the thing itself. This is generally referred to as whitespace. Whitespace is useful since it separates different things from one another.

Quick Example

Consider the following code fragment:

$hpad=floor($ipad/2);$ipad=2*$hpad;$div=$max+2*$opad;$block=$max-$ipad;

versus the spaced out version below:

$hpad = floor($ipad/2); $ipad = 2 * $hpad; $div = $max + 2 * $opad; $block = $max - $ipad;

Both execute exactly the same, but I think the second is much more readable than the first. But wait you say, if they both execute exactly the same, then what difference does it really make?

Solongasyoudon'tmindreadingtextinthisway,maybethereisn'tanypointinchangingthings.

The Rules

If you are still reading, you get my point. So, what are the rules? Again, we use practicality as a guide.

  • A blank line should separate sections of code that do different tasks
  • Spaces should separate different terms in a sequence of elements
  • Spaces should separate assignment operators in code segments
  • Each line of code should not share a line with other lines of code
  • A blank line should separate different cases in a switch statement

Notice I said should. There are always exceptions (except when there aren’t). More than anything, think about what you are writing. Someone will read it. Don’t skimp on white space. The compiler/interpretor will ignore it, but the next programmer will thank you (which could even be you).

A Note About Style

There has been much written about coding style and what is best or worst. As with so many other things, I have an opinion on the subject. My opinion has formed over the many years that I've been coding. Most of my style is language independent. That is to say, it isn't just for C or C++ or PHP or Java or any other language. These things are choices I make to improve understanding of the flow of the code from a reader's point of view. I'll give examples of each major structure with comments as to why I chose to write it the way I did.

The IF statement

if (<condition>) { <statement> } else { <statement> }

Include the opening brace ({) on the same line as the if. When you don't do this, the resulting block that follows is no longer 'tied' to the if. The relationship between the two is weakened. Same thing for the else. As for indenting, indent all the statements that are controlled by the if one level in. Braces should be at the same level as the start of the if. As I said, all of this is intended to maintain the logical relationship between the if and the controlled statements. One additional note: I almost always use the braces even those a single statement doesn't require them.

Very rarely I will use the following form of if assuming the conditions are short and the statement is as well.

if (<condition>) <statement>

The SWITCH statement

switch (<condition>) { case '1': <statement> break; case '2': <statement> break; default: <statement> break; }

Include the opening brace ({) on the same line as the switch. Indent each case one level and indent all the statements that are controlled by the case one level more. Generally include a blank line following each break;. Closing brace should be at the same level as the start of the switch.

The FOR loop (and any others of a similar nature)

for ($i = 0; $i < 50; $i++) { <statement> }

Include the opening brace ({) on the same line as the for. Indent all the statements that are controlled by the for one level. Closing brace should be at the same level as the start of the for.

Hopefully you see the pattern here. But if you don't, then let me show you another style I've seen in use. Look at these examples and refer back to mine. Then make your own choice. Just remember, you won't be the only person to ever look at this code and attempt to understand it.

if (<condition>) { <statement> } else { <statement> } switch (<condition>) { case '1': <statement> break; case '2': <statement> break; default: <statement> break; } for ($i = 0; $i > 50; $i++) { <statement> }