I'm with Zalucius and ScallioXTX (style known as ANSI or Allman)
Style B is known as Whitesmiths.
| SitePoint Sponsor |

I'm with Zalucius and ScallioXTX (style known as ANSI or Allman)
Style B is known as Whitesmiths.


Not quite. K&R and 1TBS are where classes and functions have braces on a new line, while control statements keep it on the same line.
K&R allows optional braces on control statements.Code:function func() { if (condition) { ... } }
1TBS makes braces mandatory.Code:if (condition) func();
Code:if (condition) { func(); }
Programming Group Advisor
Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
Car is to Carpet as Java is to JavaScript
Karl Austin :: Profile :: KDA Web Services Ltd.
Business Web Hosting :: Managed Dedicated Hosting :: From £250/m
Personal Web Hosting :: Budget Web Hosting :: From £50/y
Call 0800 542 9764 today and ask about our Cloud Hosting Platform


Code PHP:class Demo { function __construct() { echo 'Opening brace on same line.'; } }
In PHP I type opening brace on the same line, after a space. That's what I learned when I read my first C++ book and it has stuck with me since then. Neat indenting makes this very clean and easy to read in my opinion. I'm currently reading the book "Clean Code: A Handbook of Agile Software Craftsmanship" which I really recommend for both intermediate and advanced coders fine-tuning their habit of writing clean code.
Recently I've been using Ruby though so I haven't had to care much about braces at all.
But let's not forget what's most important of all for writing clean code; consistency. You, and your team, should settle for a coding style and go with it during the entire project. I've seen projects where different programmers use different styles and it isn't pretty.
Daniel Nordstrom. of. Nintera(ctive)
-- Featured post: Part 2. Writing NI.JS JavaScript
----- Follow me on Twitter. Got project? Contact me.
-------- SitePoint: Community Guidelines • Be A Great Member
I favour style "A", or rather a variation of 1TBS as mentioned earlier. Like @stormrider, I style classes and functions the same as other control structures.
However, with "A" the 2nd to last closing brace should be indented...
The closing brace always matches a control statement... function, if, while, etc.PHP Code:function name() {
function last() {
// Do something
}
}
I used to code in Delphi as well, although I have never mimicked the positioning of BEGIN...END with the opening/closing braces {...} as in Allmans style - I think because the opening brace on its own always looked like a waste of space to me and by including the opening brace on the same line you could see more code without having to scroll.
I actually find style "B" where you have indented the braces as well (Whitesmiths style) a bit confusing. If I was to give the opening brace its own line I wouldn't indent it, keeping it inline with the control structure (which is more like true 1TBS) ...
PHP Code:function name()
{
function last()
{
//Do something
}
}
opening brace on a new line PLEASE people!!


Programming Group Advisor
Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
Car is to Carpet as Java is to JavaScript




I don't mind the opening brace on the same line as long as the closing brace is indented so you can see what it is closing. The number of bits I code I see with this is a disgrace:
Imagine having a rather large file to debug and all the closing braces are like that. Nasty stuff that is.PHP Code:function name(){
//do something
function else(){
//do something
}
}
I'll do anything to avoid working on my own code
Are you using: if (isset($_POST['submit'])) ?
IE has a bug and does not always send the value.




I always use option A. But if it is one line conditional code. I put everything in one line with the braces on.
Ex,
Code PHP:if(userFound($username)){doThis();}
For me option B is out of the question. I don't like it at all.
------------------
Bookmarks