Poorly indented code makes me nuts. To illustrate my point, I whipped up a meaningless php script in the style of John Q. Copypaste:

WRONG

include_once('myscript.php');
function somefunc(code){$d1=1;$d2='foo'; $d3 = "bar";
   $d4 = new myscriptClass;
  $d9    =false;
foreach ($d4->aligator as $crocodile) {
     if ($crocodile['scales'] !=true){
      if ($crocodile['legs'] !=true) {
    if ($crocodile['teeth']!=true){
		echo 'not a crocodile';
	  }
		else {
			echo 'still an alligator';}
}
else{
echo "still an alligator";  }
  }else {
echo "this is in fact, still an alligator";
}}
return strtolower(code);}

  echo somfunc('ThIsCoDeSuCkS');

How could you possibly debug that mess? Proper code indentation is crucial to successfully reading, debugging, or modifying your code. There are a few different conventions, and everyone has their favorite style (for better or worse), but the most important things to remember:

  1. choose a convention and
  2. stick with it

Indentation is not required in most program languages; it is something you have to force yourself to do.

Wikipedia’s entry on code indentation lists eight styles and one variant. I won’t cover them all here, but I will provide a couple of popular examples, beginning with my preferred style.

The One True Brace Style

include_once('myscript.php');
function somefunc(code) {
	$d1 = 1;
	$d2 = 'foo';
	$d3 = "bar";
	$d4 = new myscriptClass;
	$d9 = false;
	foreach ($d4->aligator as $crocodile) {
		if ($crocodile['scales'] != true) {
			if ($crocodile['legs'] != true) {
				if ($crocodile['teeth'] != true) {
					echo 'not a crocodile';
				} else {
					echo 'still an alligator';
				}
			} else {
				echo "still an alligator";
			}
		} else {
			echo "this is in fact, still an alligator";
		}
	}
	return strtolower(code);
}
echo somfunc('This is better');

You cannot go wrong with the 1TBS. Why is this style so great?

  • Bracing occurs for every while, if, else, etc.—even when only one line of code is needed. No matter what happens to this code in the future, inserting or deleting a line of code is considered “safe” anywhere in the script.
  • Beginning braces do not take up a line by themselves.
  • The Unix kernel and Linux kernel are written in this style.

K&R Style

This is similar to my preferred style, the difference being that opening function braces are placed on their own line. Why you would want or need to differentiate functions any more than they already are is beyond me.

Allman Style

I see this one a lot on the web. All braces are placed on their own lines. I understand the proponents’ argument for white space and readability, but those opening braces annoy me. Especially on if, else statements like this:

	if ($crocodile['teeth'] != true)
	{
		echo 'not a crocodile';
	}
	else
	{
		echo 'still an alligator';
	}

Most of the other styles covered by the Wikipedia article are what I consider bad choices. The BSD KNF style incorporates too much white space. Whitesmith’s Style is a quirky, strangely perverse version of Allman. The GNU style’s odd choice of two-space indentation of braces makes for ugly code. Horstmann, Pico, and Banner styles all come highly un-recommended by yours truly, and are basically garbage.

No related posts.

Tags: , ,

Commentation