Many language constructs use braces for organizing code into related blocks. While braces ar required to group related code together, the general placement of the braces is usually determined by the programmer. Where should the braces be placed? All of the different styles have different reasons behind them. Here is a listing of those styles.
This style was first introduced by Kernighan and Ritchie’s book The C Programming Language. It puts braces on the same line as the control statements. Here’s an example:
if( $conditionOne ) { functionOne(); $variableOne = 1; } else { functionTwo(); while( $conditionTwo ) { functionThree(); $variableTwo++; } } functionFour();
The Allman style is sometimes referred to as the “ANSI style”. It puts braces on lines of their own and indents them to be aligned with the control statement. Here’s an example:
if( $conditionOne ) { functionOne(); $variableOne = 1; } else { functionTwo(); while( $conditionTwo ) { functionThree(); $variableTwo++; } } functionFour();
The Whitesmiths style is essentially the same as the Allman style, but braces are indented to be aligned with the body of code instead of the control statement. Here’s an example:
if( $conditionOne ) { functionOne(); $variableOne = 1; } else { functionTwo(); while( $conditionTwo ) { functionThree(); $variableTwo++; } } functionFour();
The Horstmann style is similar to the Allman style. The only difference is that the first statement in the code body is placed on the same line of the opening brace. Here’s an example:
if( $conditionOne ) { functionOne(); $variableOne = 1; } else { functionTwo(); while( $conditionTwo ) { functionThree(); $variableTwo++; } } functionFour();
Banner style is pretty much K&R style except that the closing braces are also indented. Here’s what it would look like:
if( $conditionOne ) { functionOne(); $variableOne = 1; } else { functionTwo(); while( $conditionTwo ) { functionThree(); $variableTwo++; } } functionFour();
This is the final style. It’s used mostly with the Pico programming language. It’s similar to the Hortstmann style, but the closing brace is put on the same line as the last control statement. Here’s an example:
if( $conditionOne ) { functionOne(); $variableOne = 1; } else { functionTwo(); while( $conditionTwo ) { functionThree(); $variableTwo++; } } functionFour();
Which style do you prefer? There isn’t a single best style. All the styles have their advantages and disadvantages. Additionally, these are styles, not rules nor regulations. Unless you work for an employer who has an established style, no one can dictate your coding style. You don’t have to use any of the styles listed here – they are just the most popular styles
My favorite style is the Whitesmiths style for ALL (PHP, JAVA, JavaScipt, CSS, etc.) coding. I find it easiest to read and practice. In addition, this style forces only one statement (including braces) per line. This I consider to be "best" practice.
You decide as what meets your needs and comfort.
Tedd
Some of the above was taken from: Austin Gulati