Coffeehouse Thread

64 posts

curly bracket placement thread

Back to Forum: Coffeehouse
  • fryguybob

    steve.x brought up the topic of curly brackets here and I thought it deserved its own thread.  So what do people at Microsoft prefer, brackets in the same column or the open bracket on the same line as an if or for?  It seems most of the documentation is of the form:

    for (;Wink
    { // Same Column
    }

    as opposed to this:

    for (;Wink { // Same Line
    }
     

    Are there Microsoft coding style guidelines? Personally I much prefer to have the brackets on the same column.

  • Manip

    With me it is either
    for (;Wink { }
    or
    for (;Wink
    {
    }
    But having the first bracket on the same line is fine.

  • Matt.Berther

    According to the C# coding style guidelines available from IDesign.Net (http://idesign.net/idesign/download/IDesign%20CSharp%20Coding%20Standard.zip), the correct style is to place the brace on a new line (section 1, item 24).

    I know this isnt straight from Microsoft, however, the document appears to have support from Microsoft. Eric Gunnerson posted this to his blog at: http://blogs.msdn.com/ericgu/archive/2004/01/19/60315.aspx

  • Robert Hess

    My own preference (which does not appear to be shared by others) is for:

    for (;Wink {
    }

    and:

    if () {
    } else {
    }

    -Robert

  • Jeremy W

    I grew up programming:

    if ()
    {
        // hello there
    }
    else
    {
        // bye
    }

    But I've had to abandon that depending on the project I'm working on. It's always made it very readable for me.

  • LazyCoder

    Count me in the "Bracket goes on a new line" crowd. I maintain that style in my C# code, JavaScript, and even in my stylesheets.

  • Sabot

    Hey I'm a column man just cos I'm old and my eyes don't work so well.

  • irascian

    Yup, another vote for "column" here, although if there's just one line I cheat and put both braces on one line - saves a lot of vertical scrolling when you have lots of property get/set ers.

  • GooberDLX

    I personally am a new line fan. I was forced to use such style during my academic career... it grew on me.

    Jake

  • rheaney

    New line is the way to go. It just looks cleaner and better.

  • gmiley

    I always preferred:

    if(){
        //foo
    }else{
        //bar
    }

    As long as there is indentation anyone should have no problems following the code.

    I know it's a weak and unfounded comparison, but I always likened putting the curly on a new line to writing:

    If Something = Whatever
    Then
        //foo
    Else
        //bar
    End If


    Which I guess would be fine if compilers let you do that...

    As far as editing someone elses code goes, I usually follow whatever scheme the current document is going with.

  • jj5

    I do it this way.

    for (;Wink {
    }

    and:

    if () {
    }
    else {
    }

    (same for methods, i.e. not KNR style (mostly because VS.NET doesn't support it))

    If I have one statement in the block then I use no blank lines, e.g.

    for (;Wink {
      this.DoSomething();
    }

    If I have more than one, then I use blank lines too, e.g.

    for (;Wink {

      this.DoSomething();
      this.DoSomethingElse();

    }

    I always close curly braces without a blank line. I have a heap of other rules too.. Tongue Out

    I use 'two spaces' instead of tabs for indenting (religiously).

    Example:

    public void SomeMethod(Int32 someParam) {

      if (someParam < 10) {
        return;
      }

      for (Int32 i = 0; i < someParam; i++) {

        this.DoSomething();
        this.DoSomethingElse();

      }
    }

    My way is of course the best, and all other ways suck!

    John.

  • PeteBeech

    Definitely a column man myself - I've always thought it looked more logical, cleaner and for me a lot more readable.

    I've often thought that there might be a correlation between how much people are into Microsoft, and their brace style (Microsoft->Columns). Based on my (totally subjective and non-scientific) observations over the years, this theory seems to hold up quite well!

    Could be good for a poll..

  • object88

    I personally perfer this:
    if (x) {
       this.DoSomething();
    } else {
       this.DoSomethingElse();
    }

    ... because it saves vertical space, and you can tell the expression block by indentation.  However, I will happily go along with the project style guidelines for constistancy sake, so I'm currently putting all my curlies on newlines.

    However, if I ever see this:
    if (x)
       this.DoSomething();

    ... I will personally smack the culprit with a halibut.  That's just bad.

  • michaeltal

    New line!!!

  • Charles

    fryguybob wrote:

    Are there Microsoft coding style guidelines?


    Yep. The official recommendation for curly brace placement in C# code for Longhorn is new line.

  • Karthik

    I'm a same column fan, but with spacing.

    I personally would prefer -

    for (;Wink
        {
              //foo
        }

    And even if it is a single line, its quite disconcerting when people use -

    if (foo)
        { //bar }
    else if (foo)
        {
        //bar1
        //bar2
        }

    That is so wrong - if I ever saw someone do that, I'd personally go down and smack them. I think consistency in coding is a good thing.

  • Charles

    fryguybob wrote:

    Are there Microsoft coding style guidelines?


    Yep, and, for managed Longhorn code, the recommendation is (drum roll, please) New Line. Smiley
    I like this because I have always used the new line curly brace pattern. 

    Keep on posting,

    Charles

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.