Why is it considered a bad practice to omit curly braces? [closed] Why is it considered a bad practice to omit curly braces? [closed] c c

Why is it considered a bad practice to omit curly braces? [closed]


Actually, the only time that's ever really bit me was when I was debugging, and commented out bar():

if(foo)  // bar();doSomethingElse();

Other than that, I tend to use:

if(foo) bar();

Which takes care of the above case.

EDIT Thanks for clarifying the question, I agree, we should not write code to the lowest common denominator.


Speed of reading...

Aside from what has already been mentioned. At this point, I've already been conditioned to parse if statements with braces and white space. So I read:

if (condition){    DoSomething();}DoSomethingElse();

Slightly faster than I read:

if (condition) DoSomething();DoSomethingElse();

I read it a little slower if it looks like this:

if (condition) DoSomething();DoSomethingElse();

I read this significantly slower than the prior:

if (condition)     DoSomething();DoSomethingElse();

beause I can't help but read it again just in-case and wonder if the author intended:

if (condition){    DoSomething();    DoSomethingElse();}

Already covered in general, but when it comes to reading the below, I'll be looking into this for quite a while to make sure what the author intended. I may even hunt down the original author to confirm.

if (condition)     DoSomething();    DoSomethingElse();


If it's something small, write it like this:

if(foo()) bar();

If it's long enough to break into two lines, use braces.