Sign in to follow this  
  • entries
    56
  • comments
    80
  • views
    41534

The ideal brace alignment

Sign in to follow this  
bladerunner627

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?
Sign in to follow this  


10 Comments


Recommended Comments

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.

Share this comment


Link to comment
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.

Share this comment


Link to comment
#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.

Share this comment


Link to comment
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.

Share this comment


Link to comment
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.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now