Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

  if (foo)
     bar; baz;
Is the above code example, though legal, considered bad practice?

Yes, misleading indentation like this is bad practice for anyone who believes that there is such a thing as "bad practice" exists. For clarity to those who do not know C, the problem is that "baz" is executed unconditionally, even though the formatting misleadingly implies that it depends on "foo".

Unfortunately (in my opinion) there is not consensus on whether the following is bad practice:

  if (foo)
     bar;
My belief is that this formatting should be avoided, to avoid the case where someone not familiar with the rules of C (or not thinking about them) edits it to add a manually indented "baz" on the next line:

  if (foo)
     bar;
     baz;
Personally, I'm fine with a single line without braces:

  if (foo) bar;
But I believe that as soon as the body is moved to another line, braces should be required:

  if (foo) {
     bar;
  }
This belief is common, but not universally shared. Some go farther and say that braces should always be required (there is a good argument for this). Others say that a two line if statement without braces is just fine (I think they are wrong).


Thanks for your informative answer.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: