Curly Brackets

Published 25 February 04 09:19 PM | alexcampbell

My co-programmers and I have been talking about coding standards within our organisation (the conversations tend to be very civilised as there are only three of us!)   The most contentious point has been whether curly brackets for procedures and if/for/foreach/do statements should begin on the same line as the statement or on the line below.

i.e. is this:

public class myPage : System.Web.UI.Page
{
     protected void Page_Load(object Sender, EventArgs e)
     {
          if(Request.QueryString[“category“] != null)
         {
               Response.Write(Request.QueryString[“category“]);
         }
     }
}

better than this:

public class myPage : System.Web.UI.Page
{
     protected void Page_Load(object Sender, EventArgs e) {
          if(Request.QueryString[“category“] != null) {
               Response.Write(Request.QueryString[“category“]);
         }
     }
}

I am inclined to think the former is better because you can immediately tell whether every curly bracket has been closed.  The consensus here, however, is that the latter is better because you can immediately tell whether something is a class or an if statement.  I'm interested in different justifications for different coding standards so anyone has any ideas please post them to the comments section of this post.

Comments

# Johnny Hall said on February 25, 2004 06:52 AM:

In my C days, I always used the former style. Now, I believe the latter is considered to be good form, and that's what I use.

[I don't use {} with a one line block though.]

# Colin Ramsay said on February 25, 2004 07:57 AM:

I'd pick the first for two reasons - consistancy and readability. Having stuff well spaced out makes things look a lot more ordered to my eyes.

# Phil Scott said on February 25, 2004 07:59 AM:

I prefer the second one myself. The only time I ever use the first one is if I'm writing some code that I'll be putting in a print out or something, so I try to conserve space.

# Paul Wilson said on February 25, 2004 08:08 AM:

I link the second format because I believe its important to encourage methods to fit on one screen. That's a good practice since it is not only readable, but it also forces you to refactor and encourages reusability. The first curly format makes this pretty much impossibly to achieve, and I think encouraging good practices, like small reusable blocks, should beat out silly format rules any day!

# Wes said on February 25, 2004 09:35 AM:

I use the first format. I think it is much easier to read then the second, at least for me. I don't like trying to hunt down the matching opening brace at the end of some line.

# GUi said on February 25, 2004 09:48 AM:

normally, I do the following:

public class myPage : System.Web.UI.Page
{
protected void Page_Load(object Sender, EventArgs e)
{
if(Request.QueryString[“category“] != null)
{
Response.Write(Request.QueryString[“category“]);
} // if
} // Page_Load
} // myPage

# GUi said on February 25, 2004 09:48 AM:

Ops, wrong identation...

# Ryan Heath said on February 25, 2004 10:12 AM:

I use the first format. I also like spaces & empty lines, for me, it makes the source more readable, like;

public class myPage
{
void Page_Load( object Sender, EventArgs e)
{
if ( Request.QueryString[“category“] != null)
{
Response.Write( Request.QueryString[“category“]);
}
}

void SecondFunc()
{
for ( int i = 0; i < 10; i++)
{
ThirdFunc( i);
}
}

void ThirdFunc( int value)
{
}
}

Note the space after if and for.
and between i = 10;
Also note the empty line between the two methods.

Some guys in my team use the latter format and use minimum space and empty lines.

I find their code hard to read, but I wont hunt them down only and because of the densed formatting... ;)

// Ryan

# Francois Verbeeck said on February 25, 2004 10:13 AM:

I really don't like the second one. IMHO It's not readable at all. I've been coding since (only) 6 years but when i have to correct / refactor code written in the second format, the first thing i do is to clean out the mess and reformat the bracket the way i like ...

# Ryan Heath said on February 25, 2004 10:14 AM:

Hmm, is there a way to keep the indentation when making a comment? :)

# Cadmium said on February 25, 2004 11:10 AM:

The first one definatly. Trying to cram as much as possible onto one screen hurts readability.

# Tim Marman said on February 25, 2004 11:21 AM:

I personally use the former formatting, as I find it much more readable. Note: I don't put braces around single line blocks, and I use (bool)? x:y; assignment whenever appropriate.

"I link the second format because I believe its important to encourage methods to fit on one screen".

Paul - I think you're missing the point behind methods fitting on one screen. It's not the amount of space that they physically occupy, but rather the implication that it is a small, comprehensible piece of code.

I can write a big line of perl code that fits on one line - that doesn't make it readable or maintainable.

Fortunately, Whidbey renders this argument moot with its automatic code formatting (ie display the code as YOU want to see it, regardless of how it was originally written).

# IReformat YourCode said on February 25, 2004 11:22 AM:

With code folding space is no longer an issue. The first method is always more readable.

Its a disservice to those that come after you to use the second.





# Omer van Kloeten said on February 25, 2004 11:35 AM:

I use the second method for coding because:
A. There's always Ctrl+']' if you want to quickly jump to the matching curly bracket.
B. You can always split the screen in half and see the beginning of the code block, if you like.
C. Better readability. When you read, your eyes want to see text, but they also want to see some blank space. The first form, IMHO, is just too crowded.

# Rob Chartier said on February 25, 2004 12:48 PM:


Count me in on the second. The first spreads the code out too much in the much-valueable screen realestate. I try to compact as much as I possibly can. Even with properties:

public string F{get{return f;}set{f=value}}

That single line, is usually spread over..what, 8 lines? Give me a break!! :)

In the end, it doesnt really matter since VS.NET can auto-format based on your own settings. (Control-A, Control-K-F)

-Rob

# Bruce Williams [MSFT] said on February 25, 2004 09:04 PM:

Please, don't even ask. This is one of those things that is best decided by executive fiat, since you'll never get agreement from a group of more than 1 programmer. Tools to automatically format code to a particular standard are also useful.