Most C language descendants and variants use braces to indicate scope, and where there is a possibility for variation there will be as many opinions as programmers as to the “correct” way to do things. There has certainly been more than enough written about where to put an opening brace, but I was having a discussion on this topic recently and just couldn’t resist adding my opinion to the noise.
First, let’s review the various conventions. There are a bunch, but they generally boil down into the following three categories:
Same Line
This style puts the opening brace for a class, method, etc. on the same line as the statement that starts the new scope. The closing brace goes on it’s own line at the same indent level as the opening statement.
class MyClass { int var = 0; public MyMethod(int arg) { arg = arg + 2; if(arg == 4) { DoSomething(arg); } DoSomethingElse(arg); } }
New Line
In this style, the opening brace always goes on it’s own line at the same indent level as the statement that starts the new scope.
class MyClass { int var = 0; public MyMethod(int arg) { arg = arg + 2; if(arg == 4) { DoSomething(arg); } DoSomethingElse(arg); } }
Hybrid
Many conventions mix the two styles above based on circumstance. For example, braces that open a class or method go on a new line while interior braces such as for loops or conditionals go on the same line.
My Preference
With that out of the way, which style do I prefer? I lean very heavily towards always putting the braces on their own line, regardless of situation. I have a couple of reasons for this. One, I don’t find modern monitors or development environments to be cramped or hurting for space. The loss of a line every now and then to a brace doesn’t really impact my ability to gain a big-picture view of the code I’m looking at. I also tend to dismiss the hybrid approaches because I don’t want to have to remember which situation is which when I’m writing code. The meaning of a brace (to indicate scope) is generally universal and so I think it’s usage should be too.
I do see real readability benefits to putting the brace on it’s own line. For starters, I find the brace symmetry to be easier on my eyes and easier to skim. When I’m quickly scanning a file with 2,000 lines of code and I need to get a handle on the structure, that symmetry really helps me form a mental image of how things line up. Perhaps more importantly though is the white space the opening brace leaves. By creating an essentially blank line both before and after the code within a given scope, the body is set apart and becomes it’s own unit. I know some folks say that indenting/spacing serves the same purpose, but I just don’t get a quick an impression without that near emptiness in the code file.
So is my way the right way? It’s worth noting that there is no right or wrong answer to where your braces should go. While the question seems to elicit the same responses from programmers as if you were questioning their mother’s monogamy, the really important thing to remember is to always be consistent. I can’t stress this enough. If you’re on a team and you like one style but the team as adopted a different one, guess what? You’re going to have to change your ways. Consistency in code is one of the best ways to ensure everyone working on a large code base can reach optimal understanding in a minimum amount of time.