• entries
56
80
• views
41534

The ideal brace alignment

187 views

I was recently browsing some source code to some open-source projects, and I noticed that most of the developers prefer different brace alignments and use them quite consistently, but I've always wondered why THAT brace alignment why not THIS one?

The following are the three most common brace alignments that I see :

if ( condition ) {    ...}if ( condition ){    ...}if ( condition )   {   ...   }

Personally I prefer the second, mainly because after many levels of comparisons you can see exactly where you are because the braces line up. I know the first brace alignment is the Java 'standard', but I really see no reason why I'm supposed to use that one - In fact, I think that one and the last brace alignment example are nearly unhelpful.

Why would you want to put the curly brace on the same line as your comparison? Does it help you in any way? I don't see how.

As for the last alignment example, I just think it's plain ugly.

So, what brace alignment do YOU use and why?

I use the second one, and I can't stand the first or third one. The XNA Game Studio Express always has the starting code with the first one, so I always take a few minutes to put the braces the way I like them.

The second one is the only one that seems logical and easy to follow, so that's why I use it.

I also use the second one so I can have the brackets aligned. But I'm fairly happy with using the first case if need be; the main argument in favour of that style is it saves space, allowing you to view more code on the screen at once. Personally I like lots of space, but that's just me.

The third style is just plain weird.

First, because that's how K&R did it. This "brace on new line" crap is just wasting whitespace. [wink]

#2 for me

#2 for me too. It just makes sense. [wink]

#2. "Wasting whitespace" was a problem back when monitors had 24 lines of text, but on today's ginormodisplays, it's more useful for me to have the braces line up in an easy-to-see manner. Also, I sort of like the way there's a bit of a "Break" created by having the brace on its own line. It helps the code "breathe" a bit.

i use the 2nd one because it just makes it easier to see. Sometimes for the first one you cant tell if its there or not at quick glance. and i use Java.

#1

Quote:
 Original post by Drilian #2. "Wasting whitespace" was a problem back when monitors had 24 lines of text, but on today's ginormodisplays, it's more useful for me to have the braces line up in an easy-to-see manner. Also, I sort of like the way there's a bit of a "Break" created by having the brace on its own line. It helps the code "breathe" a bit.

Yea, I tend to space my code out quite a bit aswell, I don't think it's a bad habit, it makes it easier to see where the larger sections lie.

Second one. Its easier to read.