Compartir a través de


Curly brace positioning redux.

I can't say I didn't enjoy the conversations around this question - it's just that I only got two comments on my actual question. People that want history can read the previous post - for all others (including any who want to "re-comment"), the question is simply this:

What do you think of this brace placement style. For the sake of this discussion, I don't want to hear about other styles you like, I just would like your comments on this particular example (clear enough?).

if (var < max)

{
DoSomething();

DoSomethingElse();
}

To be clear, I do really enjoy conversations about this highly controversial subject - I just want to target my answers in this particular case.

Comments

  • Anonymous
    February 13, 2005
    Looks good. The advantage is that all elements of the block are at the same indentation level so that when you cursor up and down the braces are easily matched with the enclosed statements.
  • Anonymous
    February 13, 2005
    I do not like it, for reasons I cannot quantify, but I can see the argument for it; everything involved is indented to the same level.

    To be fair, I hate curly braces altogether; you can't tell which open/close pair goes together, unless you comment them, and then you might as well have typed End If or Loop or whatever.
  • Anonymous
    February 13, 2005
    I've only seen it used a couple times, but it feels wierd.

    The condition sticks out visualy, but the braces stop looking like they are part of the condition.
    Being at the same level as the substatements makes it appear that that braces are part of the substatements
  • Anonymous
    February 13, 2005
    I don't like it. The beginning of the if statement is really the "IF" in my mind, and it should be on the same indentation level as the end of the statement "}". Doing that is basically doing this:

    Begin Statement
    <tab>Code block
    <tab>End Statement
  • Anonymous
    February 13, 2005
    The worst of the avail;able options!! A lot less clear than any other option I've seen!
  • Anonymous
    February 13, 2005
    The comment has been removed
  • Anonymous
    February 13, 2005
    The comment has been removed
  • Anonymous
    February 13, 2005
    I agree with Rick ("Rick", not "Rick Schaut"). He put it quite well. The if starts it and then I look down to the next }.
  • Anonymous
    February 14, 2005
    The comment has been removed
  • Anonymous
    February 14, 2005
    +1 Rick and CN:
    a) Begin and end constructs should be at the same level
    b) it just looks freaky man!

  • Anonymous
    February 14, 2005
    i bet you are on the ce team.
  • Anonymous
    February 14, 2005
    I see the argument for it, but it's still my least favorite of the various styles.
  • Anonymous
    February 14, 2005
    Doesn't work for me. I never liked that blank space after the opening/closing brace (BDS Style). Indenting the code block is enough, to set it off as something "special".
  • Anonymous
    February 14, 2005
    I do not like it, for the brakets to me is a way of doing a code block as part of the if. Now that is the way I would do VB code but it has an End If as well which is basically the same thing to me as putting in the brackets which signify a closing statement.

    So in VB this would look something like this(Hope the Spaces come across but the end if is spaced with the DoThis)
    If Something = Something Then
    DoThis()
    End If

    You loose track of your visual end if or end of code block. Traditionally over time languages have just begun to do this as a best practice as you can imagine this in a class of several thousand lines you could very quickly get lost on where the code block ended.
  • Anonymous
    February 14, 2005
    If Something = Something Then
    <tab>DoThis()
    <tab>End If

    There the VB example like with tabs. Just makes it hard to litterally switch languages.
  • Anonymous
    February 14, 2005
    DO NOT LIKE. IT'S A WASTE OF SPACE
  • Anonymous
    February 14, 2005
    Don't like it because I always associate the scoping brackets with the owning statement and therefore should be at the same indentation level.
  • Anonymous
    February 14, 2005
    The comment has been removed
  • Anonymous
    February 14, 2005
    It's hard for me to visually associate the end bracket with the if statement. I don't like it.
  • Anonymous
    February 14, 2005
    I don't like it either. It gets confusing once you have a few nested conditional statements. And there's no clear seperation between the curly braces and the code.
  • Anonymous
    February 14, 2005
    I think that the most "readable" for is with the brackets at the same indentation level as the if statement.

    It just groups the code block a little more clearly. (I won't even discuss the merits of tab as opposed to spaces in my opinion ;-) ) Also if you look at the overall flow, your eye tends to follow the indentation level a little better. You see if then a bracket. If you see an if then a statement (no bracket) then you would expect that statement to be indented immediatly but it still groups the If and statement together.

    Example:

    if (something)
    Statement;
    Next Statement;

    Better To Me:

    if (something)
    {
    Statement;
    Statement2;
    }
    Next Statement;
  • Anonymous
    February 14, 2005
    Stupid HTML Spaces... It should have looked like this:

    Example:

    if (something)
    &nbsp;&nbsp;&nbsp;&nbsp;Statement;
    Next Statement;

    Better To Me:

    if (something)
    {
    &nbsp;&nbsp;&nbsp;&nbsp;Statement;
    &nbsp;&nbsp;&nbsp;&nbsp;Statement2;
    }
    Next Statement;
  • Anonymous
    February 14, 2005
    Curly braces do, in fact, cause cancer.
  • Anonymous
    February 14, 2005
    You can put me in the "don't like" camp. That style feels as if the block delimiters are being given the same visual scope as the statements which it contains. A curly isn't one of those statements, it simply contains them, hence why I don't feel they should be in the same indentation.

    Or... curly braces are like a hug and when you give a hug you don't rip your arms off and place them on either side of the hugee... they extend out from you and around. Weird image but it's working for me grin.
  • Anonymous
    February 25, 2005
    I'll say one thing for it. It is probably the brace style with the most polarized opinions on it. If you want arguments, choose this one.
  • Anonymous
    April 03, 2006
    I think I like it.

    I like it because the treatment of

    if (condition)
    <indent>block

    is completely consistent, regardless of whether "block" is a single statement or a set of statements in braces -- all of "block" is indented to the same level, and so obviously "goes together".

  • Anonymous
    June 20, 2006
    I'd never use it because I've been using one of the other styles too long, and it would take weeks/months of sloppy code to switch over, and let's face it, style wars are the refuge of those with too much time on their hands.

    But I think it looks good, and would encourage you to use it on the off-chance I might have to maintain your code some day.

    What I think is really interesting is the comments: about how the braces "are part of" the if, when in fact they are part of the code-block following the if.

    If such a brace-style were to encourage such ignorami to actually learn the languages they use, then it can only be A Good Thing !