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

This is why you always use braces, even when they're optional. Imo a single-line if without braces is an accident waiting to happen.


This one has always been mysterious to me; I have never, in 20 years of programming, ever made this mistake; nor have I ever had to fix a bug that was caused by it. I generally do insert the braces anyway on the basis of almost a cargo-cult theory that someone else might make this mistake, but only because of received wisdom floating around on newsgroups an the like.

Mind you, much of the C code I've co-maintained for the past while is compiler source, and compiler developers may be more attuned to seeing structured text as a parse tree.


I have never, in 20 years of programming, ever made this mistake

Good for you!

But fortunately you put the braces in anyway, because the person who touches the code after you might be more prone to the mistake.

I almost never misspell things. I might therefore be tempted to believe that spell checkers are a pointless and annoying waste of time. But not everyone has the same brain as I, and experience has taught me that there are lots of people who can't spell well, many of whom are actually really good writers. I'd assume that the optional-braces problem is a cliche for a similar reason.


I agree with you here. Never happened to me, and I've never seen it happen.

Another one is if(0 == some_thing). It seems clever and I try to use it, but if you write if(some_thing = 0), the compiler just warns you. So there's no real point in doing this.


IME, working with large groups of developers on a correspondingly large project where quality varies widely over different bits of code was the quickest way to learn to be grateful for such little syntactic annoyances that however helped catch bugs (or better, prevent them) quickly.

The problem with compiler warnings is, unless you have a warning-free base build, they're difficult to see. And in turn, a warning-free build more or less depends on the very first developers to have turned the warning flags on while they were checking in stuff like mad trying to get someting to ship.

So, yeah, I could claim that I don't too many such silly mistakes (and that might even be true), but I can both understand and approve of such coding standards.


Luckily, I've never worked on a non-open-source C project with anyone but myself. So my builds are warnings clean :)


I fixed a bug in ldap2dns because of this type of error, IIRC, it had to do with SRV records.


My rule is that a simple conditional without braces can go on one line:

    if (not_true) return;
Anything longer goes on the next line, within brances.


My rule is if it's all on one line (even if complex) no braces. But if it's two lines, or if there is an else, then put in the braces.


I have to agree missing braces in general bugs me and often causes accidental bugs.




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

Search: