Do not unnecessarily use braces where a single statement will do.
if (condition)
action();
[0] https://www.kernel.org/doc/Documentation/CodingStyleI think a good language shouldn't have braces to mark blocks in first place. Given indentation,they are redundant most of the times and they just contribute in clunk. This is exactly the case with Python and hence this is essentially a default style and people hadn't be complaining about it's causing bugs.
C:
if (condition)
statement_1();
statement_2();
Python: if condition:
statement_1()
statement_2()
Personally, I always use braces in C and C++, even though it is more clunky. I want the assurance. I also frequently have to make changes to code that does not use braces, and then I have to add the braces in because I am adding statements to a conditional. To me, that is more clunky.Wrt. using { }, I omit them if I put the block to be executed on the same line, and usually I do that only with special cases, e.g.
if(expr1) continue;
if(expr2) throw new RuntimeException();From this day forward, let us proclaim to always use brackets; so that intent is more obvious to the reader.
int main() return 0; int main(argc, argv)
int argc;
char **argv;
{
int local;
}If you're worried about bugs, there are other things in C/C++ to criticize first ;)
if expression1 expression2
it can be fiendishly hard to determine the boundary between expression1 and expression2.Whether or not it's sloppy is up for debate and just a matter of personal preference.
I personally like being able to do it since it allows me to do away with the 2 extra lines auto indent puts in if i add brackets. That's a 50% reduction for a 4 line if. Maybe I should just buy a bigger monitor.
https://github.com/owen2/little-braces
Also, it is available from the online add-in manager if you search for "little braces." The VS2013 community edition is just in time :)
I couple it with the indent guideline plugin for best effect (braces are super small, light lines to track indent level, 2 space indent...).
Not _line_, _statement_. Consider
if(flag)
foo(); bar();
and if(flag)
foo =
bar +
baz;
That first example always calls bar().Warning: I haven't tested this, and am beginning to doubt a bit. It must be correct, but why, then, don't I remember seeing this in underhanded C contests? Combining that with macros allows you to hide the semicolon.