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