Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Objective-C

Case Solved: The Correct Placement of Brackets

2.65/5 (11 votes)
11 Aug 2013CPOL2 min read 31.8K  
An in-depth explanation of correct bracket placement and why...

Introduction 

It's been an on-going battle for ages..."WHERE DO THE BRACKETS GO?!?" Well, despite the popular belief that it's just up to taste, there is actually a correct answer, and you'll see why...

Background

There are 2 major types of bracket placement styles.

K&R

C++
if (this) {
  doThis();
} else {
  doThis();
}
...and Allman
C++
if (this)
{
  doThis();
}
else
{
  doThis();
}
The battle of who's right and who's wrong has been going on for ages, and I'm gonna settle this with facts. Firstly, I'm gonna start by saying from day 1 I had always been a K&R enthusiast. I found Allman to be annoying and wasteful. I'm the type of guy that likes to see as much code at once as possible so as to get a large picture of what it is I'm doing.

But recently, I became interested in making my own programming language. I ended up spending many many hours googling specifications, articles, viewpoints, and pro's and con's of dozens of languages. Then, at one point it dawned on me...K&R...IS WRONG.

Why?

First, we need to back up a bit and examine what all the instructions that can use brackets do without them.

If executes the next instruction if a statement is true. Else executes the next instruction if the previous if statement was false. For and while both repeat the next instruction while a statement is true.

Now, examine what it is they're executing in brackets without the instruction itself included.

C++
{
  doThis();
  andThis();
}     

Forget this is even part of an if/else/else if/for/while and second, and you realize this is just a block instruction. Everything inside this block belongs to the block, and everything declared within it cannot be used outside of it.
But, despite having several lines of code, this is still 1 instruction; a block instruction.

At this point, if you were a K&R enthusiast, you should be going "oh snap...", because the entire concept of K&R is based on the thought that if/else/else if/for/while are blocks, and they're not. They just execute block instructions.

But the Allman style is only partially correct as you are only indenting what's inside block instruction, not the block instruction itself. So what is the "correct" bracket/ indentation style?

C++
if (this)
  {
    doThis();
  }
else
  {
    doThis();
  } 

That's it. The block is indented because it's executed by the conditional statement, and the items within the block itself are indented because they belong to the block. An editor with block collapsing support would best handle these by allowing the user to collapse conditionals and blocks separately.

That said, I actually prefer the Allman style now despite once being a K&R enthusiast because it's close to the correct thing and double-indenting doesn't appeal to me.


So there you have it! Opinions? Criticism? Feel free to share! I love a good debate ;)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)