Quick survey on curly brace positioning


I absolutely don't want to get into a debate over whether it is better to put curly braces on their own line or not, but I do have a question on indentation I'd like to throw at the masses (ok - all 7 of you).

Generally, the curly brace debate boils down to these two examples:

 

if (var < max)  {
    DoSomething();
    }

vs.

if (var < max)  

{
    DoSomething();
}

 

I recently came across a body of code that uses a variation on the second method – but to me is far less readable.  Am I missing something?  Does anyone else favor this format?

 

if (var < max)  

    {
    DoSomething();
    }

Comments (19)
  1. Anon says:

    You forgot the standard:

    if (var < max) {

    DoSomthing();

    }

    with the last curly brace matching the column of the conditional.

  2. alter ego says:

    agreed. I see that as often as the first – but I don’t care about those today as much as the last two.

  3. Whatever the convention, it must have indenting to be readable and more important than the bracketing rules, it must be done consistently.

  4. Ramsey says:

    Looks like emacs-style brace formatting. (Blech)

  5. Jesse says:

    I prefer this (compact and readable):

    if (var < max) {

    DoSomething();

    }

  6. Cale Corbett says:

    I am firmly in the

    if (var < max)

    {

    DoSomething();

    }

    camp.

  7. Pieter de Bruin says:

    Sure you can also do:

    if (var < max) {DoSomething();}

    or

    if (var < max)

    DoSomething();

    but I prefer:

    if (var < max)

    {

    DoSomething();

    }

  8. Yaytay says:

    Always indent, if you don’t indent you can’t follow the structure, so I don’t like Anon’s example (which, barring spelling, is the same as Jesse’s) – but I supect this is a problem of the HTML rendering.

    As long as you indent, go with whatever feels best at the time, and try to be consistent in a single file, but otherwise don’t worry about it.

  9. chrisbro says:

    Our team’s coding standard is the last one you mentioned (braces and code both indented one tab from the previous indentation). It took a little bit to get used to but I find it at least as readable as the style without the braces indented. Unfortunately, VS 2003 doesn’t support it and it’s been irritating enough that I’m looking around for other editors that do.

  10. Frederik Slijkerman says:

    There is a huge treatment of brace positioning in McConnell’s Code Complete, with pros and cons for all common styles. I think the conclusion was that this style

    if (var < max) {

    __DoSomething();

    }

    had the most advantages. At least I switched to that style after reading Code Complete…

  11. CN says:

    I prefer opening and closing brace on their own line, non-indented relative the if. (Like your second example.)

    IF one writes a no-braces if or similar statement, I absolutely think that the "then" part should be placed on the same line.

    So (trivial example):

    if (true) continue;

    OR

    if (true)

    {

    ___continue;

    }

    NOT

    if (true)

    ___continue;

    The reasoning behind this is that you may think that there is an opening brace on the if-line, while the combination of the missing indention and the statement on the end of the if-line should make it clear enough that it is a one-liner.

  12. Paul Wilson says:

    I like:

    if (var < max) {

    __DoSomething();

    }

  13. Apostate says:

    One consideration that I never see addressed: {} usage in contexts beyond conditionals. Try inventing a style that you can use consistently and clearly with conditionals and with initializers, especially nested initializers:

    struct s

    s const x[] = // nested initializer — how to write {}?

    and with statement blocks not subordinate to another statement or declaration:

    statement1; {

    statement2; // misleading if read quickly

    }

    statement3;

    and in class/struct definitions :

    struct s {

    int i; // misleading if read quickly

    };

    and in constructors with a long member-initializer list, if you want to list the initializers on separate lines:

    c() : m1(…), m2(…), m3(…), m4(…) {

    // alternative:

    c() :

    m1(…),

    m2(…),

    m3(…),

    m4(…)

    // where to put brace?

    The sad reality: the C++ grammar does not easily allow a consistent and readable style for all contexts allowing {}. Even if you are consistent with {} for conditional statements, are you just as consistent with {} usage in general? If not, why focus on conditionals {} usage in particular?

    I don’t know what the right answer is, or that there is a right answer. I do know that almost every time I see this discussion, it ignores the more difficult problem of {} layout in the large, in favor of {} layout in the small (conditionals only).

  14. Apostate says:

    I had some indenting, but the blog editor took it all away :^( Sorry, I didn’t mean for all of my examples to be squashed left.

  15. Dave says:

    If var < max Then

    DoSomething()

    End If

    Clearly more readable.

    Sadly the genuises who came up with the C# compiler don’t allow it.

    VB ain’t so bad afterall.

  16. James Hancock says:

    always {

    }

    I hate curlies on their own line

    if () {

    } else {

    }

    maybe it’s my vb background….

  17. I prefer

    |if (var < max) {

    | DoSomething();

    |}

  18. Donald Scott says:

    How can you make emacs’s auto-indent do this,

    if (var < max)

    {

    DoSomething();

    }

    instead of this,

    if (var < max)

    {

    DoSomething();

    }

  19. Donald Scott says:

    the last is supposed to look like the third example of bracing on this page at the top.

Comments are closed.

Skip to main content