6 ms·
Is there a reason there aren't any braces around single-line if statements? Is that a C thing? It seems kind of inviting to bugs to me.
by benihana 12y ago
Is there a reason there aren't any braces around single-line if statements? Is that a C thing? It seems kind of inviting to bugs to me.
- workingandtired 12y agoIt's a C-style language syntax option. If it's only a single line in after the if, the braces are optional. I've also seen it in C++ and PHP. Whether or not it's sloppy is up for debate and just a matter of personal preference.
- solistice 12y agoC# also has it, and i've heard Java has got it as well, but I've never tested it in the latter. 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.
- workingandtired 12y agoThe lead programmer at my last job encouraged us to use it as a way to make sure our conditionals & foreach loops (PHP programmer) weren't doing too much. If we had to use braces, it was a sign to check it out and see if it could stand some refactoring.
- tenementfunster 12y agoSounds a tad inane.
- seanmcdirmid 12y agoI use a plugin called littlebrace for visual studio, which reduces brace lines to like 3 pt font. My C# code has started looking like Python :)
- TheEzEzz 12y agoThis sounds really neat. Can you drop a link? I browsed around but couldn't find anything.
- vacri 12y agohttps://github.com/lukesdm/little-braces https://github.com/lukesdm/little-braces looks like the one.
- seanmcdirmid 12y agoAlso see this fork/port to VS2013: https://github.com/owen2/little-braces 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...).
- MichaelGG 12y agoOr use a better bracing style. Or move to significant whitespace.
- Someone 12y ago"If it's only a single line in after the if, the braces are optional." 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.
- rgbrgb 12y agoSo as to have more code on the page. It's official style of the Linux kernel [1]. [1]: https://www.kernel.org/doc/Documentation/CodingStyle https://www.kernel.org/doc/Documentation/CodingStyle
- kevin_thibedeau 12y agoIn the C grammar, braces denote compound statements. Control flow statements can take any type of statement as their body rather than just the compound variety.
- PurplePanda 12y agoIt confuses me that it doesn't work for functions. like int main() return 0;
- pdw 12y agoIn K&R C, the function braces serve to separate parameter declarations and local variables: int main(argc, argv) int argc; char **argv; { int local; }
- PurplePanda 12y agoThanks, I haven't seen that syntax before.
- desdiv 12y agoIt'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.
- infinitone 12y agoIts common in Java too...
- alexvr 12y agoMost C-inspired languages (most popular programming languages) allow this. The only ones I've used that don't are Go and Swift. And I find it a little annoying. If you're worried about bugs, there are other things in C/C++ to criticize first ;)
- desdiv 12y agoIt's true for Rust as well. What these three languages have in common is that they don't require parentheses around the switching boolean expression, and when you have things like: if expression1 expression2 it can be fiendishly hard to determine the boundary between expression1 and expression2.
- jmount 12y agoC has potentially ambiguous association on nested "else"s. The habit of too many braces is a safety.