Correctly Formatting Programming Code

Microsoft Visual Studio 2008

Microsoft Visual Studio 2008

I have agreed and disagreed with so many people about this point.There are so many arguments as to what is correct when formatting code.

I have always believed that making your code readable is more beneficial that saving disk space in reducing white space in your code etc…

The fact remains that to be able to read your code with ease is it nessecary to correctly format it.

Therefore I am suggesting a formatting style that is now native to quite a few coding & program design programs, such as Microsoft Visual Studio.

The code formatting style of subject is Allman Style.

If you take a look at the attached image (top right) you will see how neat code looks when coding in this style.

This style puts the brace associated with a control statement on the next line in the same column. Like so:

while
{
    somefunc();
    somefuncelse();
}
finalfunc();

A great advantage of this style is that the control statement is seperated from from the flow of the code, this means that you could easily do something like this:

//while(x == y)
{
    something();
    somethingelse();
}

This allows you to comment out the control statement and leave the curly braces in place. This is also beneficial in comparison to other programming styles as with the curly brace on the same line as the control statement is easy to loose track of, particularly when you have many nested statements.

I managed to find this article on PHP syntax formatting best practices and in particular, this page on code indenting.

Comments are closed.