4 ms·
It's pointless to argue over these kind of things. Every major project/company has their own codified code style guide, and if you want to contribute/earn your
by desdiv 12y ago
It's pointless to argue over these kind of things. Every major project/company has their own codified code style guide, and if you want to contribute/earn your salary then you must follow that style guide to the T. Here's the relevant quote from the Linux kernel coding style[0]:
Do not unnecessarily use braces where a single statement will do.
if (condition)
action();
[0] https://www.kernel.org/doc/Documentation/CodingStyle https://www.kernel.org/doc/Documentation/CodingStyle
- guelo 12y agoI consider that a bug in their style spec. Single line if statements are known to cause bugs.
- sytelus 12y agoThat's what I'd thought for may be over a decade. About ~3 years ago I revamped my personal coding style to eliminate as unnecessary baggage as possible. As part of that I stopped using braces for single line if and I'd yet to bump in a bug because of that. Overall I find code looks more compact and cleaner, may be even less friction to read. Nowadays when I see a braces around single line if I get that "oh that's clunky code" feeling in my stomach. Things are worse with C# and lot of Java code where people insist not only having braces around single line if but also have { on its own separate lines. I 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.
- scott_s 12y agoThere's an enormous difference with Python, because the indentation is syntax. These two code snippets, one in Python, the other in C, do not mean the same thing: 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.
- mushishi 12y agoI nowadays resist the urge to make syntactically beautiful code if that means that it is a little bit brittle or vulnerable to mistakes. 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();
- chinpokomon 12y agoI see merit in both sides of the debate. As long as the style is consistent, it should be legible. It is an argument similar to vi versus Emacs -- both can be right. It is more important to be consistent through a project. Adopt one style. From this day forward, let us proclaim to always use brackets; so that intent is more obvious to the reader.
- elwell 12y agoYou can sidestep the braces debate by using a lisp.