15 ms·
Another oddity of C that amuses me is the do/while loop without braces: int i = 4; do printf("hey\n"); while (--i > 0); Even though do/whil
by barrkel 11y ago
Another oddity of C that amuses me is the do/while loop without braces:
int i = 4;
do
printf("hey\n");
while (--i > 0);
Even though do/while is a keyword bracketing pair in C, it still only lets you use a single statement (because nested whiles). So everybody uses braces, and thus it looks quite disturbing without them.
- cygx 11y agoit looks quite disturbing without them Remove the newline and it looks ok to me: int i = 42; do printf("hey\n"); while (--i > 0); Another possibility would be: int i = 42; do printf("hey\n"); while (--i > 0);
- viraptor 11y agoOohh... this would be so good for the underhanded C contest! Just mix is up with the comments which make the `do` look like (assuming good actor) an accidentally wrapped comment. /* The following code does something, so here's the // explanations of what happens. And here's what we actually */ do printf("hey\n"); /* And now just count down */ while (some_check(--i)); Now spot that in a large file of real code!
- pierrec 11y agoSneaky. Most syntax highlighting would make it stick out like a sore thumb, but still, sneaky.
- gpvos 11y agoJust looking ok is not the point. I would prefer a match between how it looks and what it does...
- dllthomas 11y agoI agree, but I don't see that undermined here.
- kevin_thibedeau 11y agoIts not so disturbing when you consider that braces aren't special cased in the C grammar. They group statements so they can be used together where a statement is needed. From the grammar's perspective the "normal" way is without braces. It's just that all the C-alikes have gone a different direction with the way braces are parsed leading everyone to regard the original behavior in C as ugly warts.
- tmuir 11y agofor, if, and while without curly braces for single statement blocks are easily one of the worst things about C. It bites you in the ass every time. The balance between its utility and its capacity to cause bugs is so one sided, I don't understand why it's even taught to beginners. If you are teaching a new programmer that saving keystrokes is important, you're on the fast track to creating a shitty programmer.
- JoeAltmaier 11y agoThat's pretty severe. Written with spacing, its really pretty clear what is meant by if (condition) Foo(x) Braces are, in my opinion, an unfortunate necessity in some cases. They are a much larger cause of error than NOT using them ever could be. An ideal IDE would make blocking visible (background tone change etc), and braces could be emitted automatically by the IDE without ever cluttering up the code shown to the programmer.
- Igglyboo 11y agoThe only problem with this is if/when someone comes along and doesn't notice the missing braces and does if (condition) Foo(x) Bar(x) Expecting Bar(x) to be part of the conditional. This can and does happen.
- JoeAltmaier 11y agoI hear folks say that, but don't encounter it in the wild. Maybe once in 20 years so far. That construct, reading it just now, just screams out at me "Indentation error!" Anyway it nicely illustrates the need to get braces out of there altogether. The programmers' intent is obvious; let the IDE 'make it so' by emitting braces in the generated code.
- gumby 11y ago> Another oddity of C that amuses me is the do/while loop without braces: [...] > Even though do/while is a keyword bracketing pair in C, it still only lets you use a single statement (because nested whiles). Ah, but don't forget you can still use the comma operator, so get several statements in before the semicolon: int i = 4; do printf("hey"), printf("Jude.\n"); while (--i > 0);