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 (27)

  1. Doug says:

    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.

  2. Robert Gaston says:

    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.

  3. AndrewSeven says:

    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

  4. Rick says:

    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

  5. N says:

    The worst of the avail;able options!! A lot less clear than any other option I’ve seen!

  6. Rick Schaut says:

    I’m probably biased, because this is the indentation style used in Office, but I’ve come to prefer it over others. The indentation of the braces matches the indentation you’d use for a single statement within the scope of the controlling line. Syntactically, the block is the same as a single statement, and I find I like having this syntactic equivalence reflected in the indentation itself.

    As for readability, I rarely read code line-by-line. I first want to get a high-level view of the logic and flow of a function before getting into the details. Indenting the braces leaves conditional statements isolated within their own indentation level, which makes it easier to get this high-level view.

    All that said, your mileage may vary, and their are entirely valid, though highly subjective, reasons for preferring any indentation method.

  7. alter ego says:

    Great comments. I’ve got to say that the style has grown on me (although it still looks "funny" or "weird".

    I’ve grown accustomed to the diagonal lines that the non-indented braces create when reading blocks of code, but as I browse more and more code written in this manner, I’m beginning to see similar patterns that allow me to see flow without reading line by line.

  8. CN says:

    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 }.

  9. Thasmudyan says:

    Technically it probably makes the most sense. The entire code block in the braces should be on the same level, with the "if" statement launching it. Personally, I think BSD-style is prettier and (only a little less) logical too:

    if (foo)

    {

    <tab>statement;

    }

    But that’s just taste. Anyway, the main thing is the braces need to visually correspond to another, and the indentation must be able to convey some idea of the program flow.

    I think it’s great!

  10. Simon Smith says:

    +1 Rick and CN:

    a) Begin and end constructs should be at the same level

    b) it just looks freaky man!

  11. Adam says:

    i bet you are on the ce team.

  12. Beta says:

    I see the argument for it, but it’s still my least favorite of the various styles.

  13. George says:

    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".

  14. Jeff Parker says:

    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.

  15. Jeff Parker says:

    If Something = Something Then

    <tab>DoThis()

    <tab>End If

    There the VB example like with tabs. Just makes it hard to litterally switch languages.

  16. DO NOT LIKE. IT’S A WASTE OF SPACE

  17. Drew Marsh says:

    Don’t like it because I always associate the scoping brackets with the owning statement and therefore should be at the same indentation level.

  18. Paul Wilson says:

    It sucks. There’s no closing bracket indented the same as the opening statement to indicate that the block is done. And the opening bracket is just dead space — while I like it on the same line as the opening statement (to save space) I can at least see the argument for it being on a line by itself to delineate the opening of the block — but this placement achieves nothing.

  19. James says:

    It’s hard for me to visually associate the end bracket with the if statement. I don’t like it.

  20. Dara says:

    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.

  21. 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;

  22. 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;

  23. Jeff Atwood says:

    Curly braces do, in fact, cause cancer.

  24. Shawn Oster says:

    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.

  25. Sue Loh says:

    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.

  26. Kevan Chapman says:

    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".

  27. Alan says:

    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 !

Skip to main content