Hacker News new | ask | show | jobs
by jotux 4037 days ago
I've never seen (in code I've worked on)

    if (condition)
        Foo(x);
        Bar(x);
But I have seen

    if(condition1)
        if(condition2)
            if(condition3 && condition4)
                Foo();
This upset the old ARM compiler I was working on and it decided to skip some of the conditions. I fixed the bug, related to this code, by adding braces:

    if(condition1)
    {
        if(condition2)
        {
            if(condition3 && condition4)
            {
                Foo();
            }
        }
    }
So this is why I always put braces to define scope. Although, in general, both of these types of bugs are uncommon.

Honestly, the more pervasive problem that comes up is the eventual addition of new code adds noise to diffs. Like if I have a condition with a single statement

    if (condition)
        Foo(x);
And I add something to it, I have to add braces and it pollutes the diff with stuff that isn't really related to what I'm changing.

    if (condition)
    {
        Foo(x);
        Bar();
    }
1 comments

"This upset the old ARM compiler I was working on and it decided to skip some of the conditions."

Do what you need to work around a known compiler bug, of course, but that is definitively a compiler bug. The meaning is unambiguous and consistent in the C standard and every implementation I've encountered. I'm not comfortable with the assertion that changing your coding style here makes you less susceptible to compiler bugs in general.