Ahh, the religious war finally arrives on the DC shores
In my experience working with many developers, the K&R style is almost universally derided, while the vast majority of developers prefer the Allman style
However, more recently a couple of guys have convinced me of the philosophical superiority of the Whitesmith style
that Mouser referenced. There's no practical difference relative to Allman. But in the context of the BNF for the grammar of C or C++ (see http://www.nongnu.org/hcb/
), looking at the definition of statement
, you can see that what you're indenting is, in any case, some kind of statement
. The Allman approach would have you indenting the opening of a compound-statement at the same level
as its controlling selection-statement
. The Whitesmith style is entirely consistent, though.
From a practical perspective, either of these is superior to K&R because they allow you too comment out things like the controlling selection clause and still get a program that is syntactically correct. But with K&R-style, the opening curlybracket would have been commented out as well, breaking the code.
Let me add this to the controversy: with my style, single-statement blocks are never
legal. Any block, even if it's just a single statement, must
be enclosed in curly brackets. I've seen too many bugs that are the result of adding a second statement to what you'd like to have done in a conditional -- but since there were no curlies, that second statement is executed in all cases. Simply using curlies all the time prevents this from ever happening. (and it's also one reason why python's whitespace-based blocking is better)
And another thing: whenever possible, when doing comparisons, always try to have the lefthand side of the "==" comparison operator be an rvalue
rather than an lvalue
(that is, it's an expression that can't legally be assigned to). This prevents accidental assignment if one of the equals signs is inadvertently omitted.