ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

Coding Practices Question: Curly brackets on a new line for else statement?

<< < (3/3)

jgpaiva:
ps: I really like how eclipse formats code down to spaces in function calls, between the 'if' and the condition, between the left and right side of an attribution, etc etc... It's great to have all your code respecting the same norm, makes it much easier to understand :D

CWuestefeld:
Ahh, the religious war finally arrives on the DC shores  :o

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 and compound-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.

Deozaan:
Apparently when I started practicing indent styles, I used the 1TBS.

But since I've been working with Torque, it appears I've started using the Allman/ANSI style.

It wasn't my intent to start an indenting style religious war. This topic wasn't asking for a discussion on the superior indenting style. Now that I know these things have names, I think I can more adequately phrase my question:

How would one correctly vertically space the brackets of an if/else statement using Allman style? It appears to me that the answer is my last example in the original post.

Thanks for the feedback and information.

jgpaiva:
I don't think anyone in this topic entered a religious war... I only see constructive criticism ;)

Navigation

[0] Message Index

[*] Previous page

Go to full version