Click here to Skip to main content
15,885,209 members
Articles / Programming Languages / Objective C
Tip/Trick

Case Solved: The Correct Placement of Brackets

Rate me:
Please Sign up or sign in to vote.
2.65/5 (11 votes)
11 Aug 2013CPOL2 min read 29.1K   1   23
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Ian A Davidson23-Nov-13 11:06
Ian A Davidson23-Nov-13 11:06 
GeneralMy vote of 3 Pin
califf2226-Aug-13 7:08
califf2226-Aug-13 7:08 
GeneralMy vote of 3 Pin
Emilio Garavaglia15-Aug-13 22:04
Emilio Garavaglia15-Aug-13 22:04 
QuestionThe correct style is the most consistent Pin
holstebroe13-Aug-13 2:59
holstebroe13-Aug-13 2:59 
It makes no sense to claim that one style is more correct based on the argument that block brackets should be indented. For sure, the compiler doesn't care, so the best style is whatever style is most readable by the programmer. An important property of readability is consistency and familiarity.
Indentation is great to indicate coherence of expressions in a block, but the block brackets should just go unnoticed by the reader and not take up valuable thought resources. If the brackets are placed consistently the reader will soon start to ignore the brackets and only see the actual lines of code.

The brackets were invented in a time with limited screen real estate and primitive editors where indentation for each sub-block was not always practical. Most modern editors will enforce indentation and bracket style and many modern languages (like Python and F#) has omitted block brackets.
QuestionMy vote of 2 Pin
Marius Bancila12-Aug-13 4:37
professionalMarius Bancila12-Aug-13 4:37 
GeneralMy vote of 2 Pin
Marius Bancila12-Aug-13 4:35
professionalMarius Bancila12-Aug-13 4:35 
GeneralMessage Removed Pin
12-Aug-13 3:37
User 113800012-Aug-13 3:37 
GeneralMy vote of 1 Pin
Chris Grain12-Aug-13 2:52
Chris Grain12-Aug-13 2:52 
GeneralRe: My vote of 1 Pin
Ghosuwa Wogomon12-Aug-13 8:51
Ghosuwa Wogomon12-Aug-13 8:51 
QuestionCase Solved: The Correct Placement of Brackets Pin
john morrison leon12-Aug-13 2:07
john morrison leon12-Aug-13 2:07 
GeneralMy vote of 1 Pin
Snorri Kristjansson12-Aug-13 2:01
professionalSnorri Kristjansson12-Aug-13 2:01 
AnswerSo... you have finally found the correct color for underwear Pin
Power Puff Boy11-Aug-13 23:21
Power Puff Boy11-Aug-13 23:21 
GeneralRe: So... you have finally found the correct color for underwear Pin
Ghosuwa Wogomon12-Aug-13 9:09
Ghosuwa Wogomon12-Aug-13 9:09 
GeneralRe: So... you have finally found the correct color for underwear Pin
Power Puff Boy13-Aug-13 4:21
Power Puff Boy13-Aug-13 4:21 
QuestionClassical debate about personal opinion... Pin
StM0n11-Aug-13 22:49
StM0n11-Aug-13 22:49 
QuestionOn the right track but... Pin
Jean-Louis Leroy11-Aug-13 22:42
Jean-Louis Leroy11-Aug-13 22:42 
QuestionClose but capable of improvement. Pin
Dave Cross11-Aug-13 21:51
professionalDave Cross11-Aug-13 21:51 
QuestionThis is no where near compete - and it's a personal opinion. Pin
OriginalGriff11-Aug-13 20:34
mveOriginalGriff11-Aug-13 20:34 
AnswerRe: This is no where near compete - and it's a personal opinion. Pin
Jean-Louis Leroy11-Aug-13 22:45
Jean-Louis Leroy11-Aug-13 22:45 
GeneralRe: This is no where near compete - and it's a personal opinion. Pin
OriginalGriff11-Aug-13 22:49
mveOriginalGriff11-Aug-13 22:49 
GeneralRe: This is no where near compete - and it's a personal opinion. Pin
Jean-Louis Leroy11-Aug-13 23:05
Jean-Louis Leroy11-Aug-13 23:05 
GeneralRe: This is no where near compete - and it's a personal opinion. Pin
OriginalGriff11-Aug-13 23:12
mveOriginalGriff11-Aug-13 23:12 
GeneralRe: This is no where near compete - and it's a personal opinion. Pin
Jean-Louis Leroy11-Aug-13 23:25
Jean-Louis Leroy11-Aug-13 23:25 
GeneralMy vote of 1 Pin
OriginalGriff11-Aug-13 20:29
mveOriginalGriff11-Aug-13 20:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.