Wednesday, June 20, 2012

Brace Yourself

It seems like people always find the silliest things to fight over... sex, money, power, religion, and other such trifles. It's probably only a matter of time until a world war erupts over a really serious topic such as Where To Put The Opening Brace.
Allegedly some people think that when opening a block, the brace should appear at the end of the line that starts the block, like so:

if (theAnswer==42) {
    // what was the question again?


Whereas other people think that this little brace deserves a line all of its own:

if (food==PIZZA)
    omNomNom ();

The merits of the first style should be pretty obvious: wasting an entire line on an opening brace is a terrible sin. The second style is allegedly more readable.
I'm not going to write an entire essay about this, because I've already tried to explain that shorter is better. I'll say just mention a few short points.

  • In VB (and VBS, and VBA) you don't even get braces, and the keyword that starts a block cannot be on its own line. It's quite popular and I've never heard anyone accuse it of being difficult to read.
  • Python has no braces at all, just indentation, and it's pretty much the single most elegant and readable programming language that is widely used, and often compared to pseudo code. 
  • TCL does have braces but you're not allowed to give them their own line. This one isn't exactly popular, but the syntax is mostly quite readable.
  • There's plenty of C, C++ and C# programmers out there who manage just fine with the first style.
All I'm saying is: this really isn't as essential to readability as some people believe, and I hope they'll reconsider. This is the easiest way I can imagine to save a lot of redundant lines (7% of a large piece of code I sampled today).

Remember, a line saved is a line earned!


  1. Thats a language convension...

    1. How is it a language convention rather than a local one?
      Different groups have different conventions for these in C and C++ (and, I assume, C# and Java and PHP As well).