The correct place of bracket in programming

Basically, the location of bracket ({}) in programming is a very old argument and up to now there is no absolute winner. For those that my words look like an alien language let me provide an example. In programming languages like C/C++, Java and so on bracket ({}) are used widely. As a result you can place { in your code in two forms like following,

// Style 1
try
{
        // Do something
}
catch(Exception ex)
{
       ex.printStackTrace();
}
// Style 2
try {
        // Do something
} catch(Exception ex) {
        ex.printStackTrace();
}

As you can see, there are two common styles. Now which is one is better or more readable is different argument which I also have no answer to that, but I summarized pros and cons of each based on my understanding.

  • Style 1 is more readable and easier to catch but style 2 is not
  • Style 1 might looks like old fashioned
  • Style 1 is wastage of lines whereas Style 2 is more compact
  • Style 2 is easier for developer and make the process of coding slightly faster
  • Style 2 is more supported by IDEs

Now my final conclusion,

  • For programming languages like C/C++, I recommend to use style 1 but modern programming like Java Script, JQuery better use style 2 to save space and it is more common practice
  • No matter what forms you follow, just follow one and only one style in a project

And lastly, happy coding 🙂

Leave a Reply