Braces

Thanks to javascript, I think the age old argument BSD vs. K&R style curly braces is officially over. First, my own personal reason for using K&R. I just fixed a stupid error where I accidentally inserted a semi-colon at the end of one of my if statements. For example:

for (var i = 0; i < arr.length; i++)
{
    var foo = arr[i];
    if (!foo || bar.match(/regex/));
    {
        if (foo.baz)
        {
            arr.splice(i, 1);
            i--;
        }
        _fizz(bang(foo));
    }
}

It’s pretty hard to notice there, but it will definitely do some things you don’t expect. Doesn’t this look much more obvious?

if (!foo || bar.match(/regex/)); {

Look at that, you have a grumpy little guy ; { rather than the slightly unhappy );. Anyhow, the other reason to put the brace on the same line is to avoid a feature in javascript where it automatically inserts semi-colons for you:

return
{
   foo: 'bar',
   fizz: 'bang'
};

will return undefined instead of your object. I love it that avoiding a language feature comes around to justify my curly brace style choice.

Leave a comment

Recent Entries

  • New York City

    I spent last weekend in New York City. It was my second time in New York but the first time I really got to see...

  • Ctrl, Alt, Delete

    My life has been crazy and it looks like the weeks will only get even more hectic. I always measure how busy I should be...

  • Perspective

    Despite the lack of activity on this blog for so long, I was surprised to learn that I still am getting more traffic than before....

  • Back!

    Sorry. I was gone for a long time. Blogging is a paradox. You can never be doing interesting things and blogging at the same time....

  • Good Service

    It was my girlfriend's birthday today and to celebrate, we went out to eat at a nice restaurant in Sacramento. Sacramento had a wine-and-dine week...

Close