Brace Face

July 27, 2010 at 11:49 pm

What is this big deal about brace style? Take the following for example.

if(cond) {
    // Do something
} else if(otherCond) {
    // Do Something else
} else {
    // Otherwise
}

Does that look right? I think it is hard to follow and confusing. Allman style bracing is much more straightforward. New lines for everything, glorify that whitespace.

I had someone comment:

Why do you put your braces like that? You save a line by putting it together. It’s so much more compact, etc.

Readability. If I cared about saving space I wouldn’t comment my code.

if(cond)
{
    // Do something
}
else if(otherCond)
{
    // About your
}
else
{
    // Brace style
}

It even makes logic swaps easier (something I picked up on before wikipedia did). See the following.

//for(i = 0; i < someArr; i++)
if(someArr.length() > 0)
{
    // Do something
}

Since when has code been something we want compact? If you want everything compact, golf the shit out of it. Otherwise, make it nice. Make it readable, put your braces on new lines.

§

July 2010

Can’t find what you’re looking for? Try hitting the home page or viewing all archives.