May 142009
 

Among C-ish programmers, there is often dispute which brace style to use. K&R style (named after authors of The C Programming Language)

void Main() {
  Console.WriteLine("Hello World!");
}

was very popular once, but Allman style
void Main()
{
  Console.WriteLine("Hello World!");
}

is what seems to be popular now. Basically only difference is where to put starting brace. Whether it stands behind control statement or it stands on dedicated line. And that is something that people cannot decide for quite a while now. Both of them have their advantages that I will not discuss here (there is pretty good article on Wikipedia).

I am currently using K&R style. Here I will try to make two points why everyone should switch to Allman.

Visual Studio defaults

Default brace style for C# is Allman. If you need to modify code written by others, that code will rarely use K&R. While Visual Studio and C# do give you great options of reformatting code, I consider doing that just being rude.

I was known to change formatting of code in other people files and it took me a while to understand how unreadable whole project becomes when half of files use one style and half another. Now, if project has established style, I will continue adding stuff in same style.

Readability

If you are used to code in one style, you are better at scanning code in that style. If you get Allman code, it helps to be used to Allman to read it faster. It does not hurt too much if your code preference is different, but there is some speed advantage.

Conclusion

I can see that I should code in Allman all the time. I know it would be beneficial. But I cannot force myself to do it. Although all reasoning says that Allman is more readable, it seems that my mind is just not used to it. It just likes to find them by indentation rather than by matching braces.

Maybe it is because I have history in VB which looks quite like K&R in indentation style, maybe it is because I never gave Allman more than a week to get used to, maybe it is just because I like to be different.

Against of all reasoning – I am sticking with K&R for now.

  One Response to “Which brace style to use”

Comments (1)
  1. To me as a beginner in C++, was more logical and natural to put opening braces on next line, no matter that examples were mainly written K&R style and that was also style my teacher was using. Than I didn’t know about coding styles, how many are there and their names.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>