Not sure I follow you on the one; two; example. Why would you have two statements per line in the first place? (You do realize that the "two;" isn't in the if block, right?)
Not sure I follow you on the one; two; example. Why would you have two statements per line in the first place? (You do realize that the "two;" isn't in the if block, right?)
if (statement) one;
And developper two added
if (statement) one; two;
too quickly
If you had a coding convention of always using braces in if statements, you might be insulated from this particular symptom or instance of the problem, but you still have the underlying problem that people are doing things without thinking.
You can solve this problem?
People are fallible, mistakes get made.
The actual argument has been made in sibling comments: braces are verbose, and the mistake we speak of is exceedingly rare in practice. For simple one-liners, the verbosity costs more than the lack of safety net.
A lint tool checking for suspicious indentation would be a better use of labour.
These types of conventions -- no curlies around simple blocks -- aren't too bad if a) your devs are adequately seasoned, and b) your devs believe in whitespace.
For instance, this isn't really so bad:
if (foo)
foo();
else
bar();
Hell, that's clean!But it's stuff like this that causes issues:
if(foo)foo();
else bar();
Sometimes I'm lucky and find this: if (foo)
foo();
else {
bar();
baz();
}
The best is abominations like this though; first, the sane formatting: if (foo)
while (i++ < x)
foo(i);
else
bar();
Or, as I have found it: if(foo)while(i++<x)foo(i);
else bar();
Seriously, that's enough for me to revoke commit privs. if(flag)
set_other_flag=1;
call_function();
(both lines necessary)Anyway, it didn't take long to narrow it down to that bit of code but it took a few more minutes to figure out why the function was always getting called. I couldn't work out what was going on until I looked at the disassembly and was forced to reassess my opinion of what code was actually being compiled. Too much python in my diet, perhaps.
I remember being surprised at the time that it had taken so long for this to happen, and I made a mental note to keep an eye out for more occurrences. That was summer 2006, and I haven't seen it happen since.
#define one a;b;
if (statement) one;