Jon Aquino's Mental Garden

Engineering beautiful software jon aquino labs | personal blog

Thursday, December 21, 2006

Language-agnostic indentation

Just wanted to share a style of code-indentation that I've found to express clearly the logic of if statements and other control structures inside template files. That is, when PHP and HTML are mixed together, this technique can make it much clearer what's going on (in terms of nested structures, such as if statements, for loops, etc.).

You simply indent with each level of nesting, regardless of which language you are in; for example:

<li>Quarter Pounder</li>
if ($very_hungry) { ?>
<li>French Fries</li>
} ?>

This makes a dramatic difference with complex logic. For example:

Screenshot: Before

That if statement on line 18 - is it inside the for loop or not? Is it inside any other if statements?

Compare with:

Screenshot: After

Here the nesting is clear.


Post a Comment

<< Home