tags:

views:

378

answers:

6

For anyone who places braces thus:

void f() {
    stuff();
}

How do you prefer to place braces after long initializer lists?
The same way?

Object::Object()
    : foo(1)
    , bar(2) {
    stuff();
}

Or make an exception so you actually see where the init list ends?

Object::Object()
    : foo(1)
    , bar(2)
{
    stuff();
}

Or leave a blank line?

Object::Object()
    : foo(1)
    , bar(2) {

    stuff();
}

Or maybe make a weird hybrid?

Object::Object()
    : foo(1)
    , bar(2)
    {
    stuff();
}

Or abuse indentation

Object::Object()
: foo(1)
, bar(2) {
    stuff();
}

Object::Object() : foo(1)
                            , bar(2) {
    stuff();
}

In this small example all are pretty but crank a dozen initializers and a moderately long function body and this quickly changes.

+4  A: 

It seems important to be able to detect where the ctor-initializer list begins/ends, and where the code block begins/ends.

Object::Object ()
  : foo (1)
  , bar (2)
{
  ...
}
Paul Beckingham
A: 

I would guess that most developers like to remain consistent with their bracket formatting with how they are using it in the rest of their code. It's hard enough to read a bracket format that is not your own style. Trying to decipher a mixed format would make your program even more unreadable to someone who is not familiar to your code.

matt_dev
+2  A: 

I like to have as much code on the screen as possible at once, so that my eyes can jump back and forth with as little scrolling as possible. There is, of course, a limit. Once I start needing to hunt too hard for something within a line, I have reached a point of diminishing returns. If I'm not initializing more than 3 or 4 fields, I usually do something like this.

Object::Object() : foo(1), bar(2) {
    stuff();
}

If I need to initialize lots of fields, it tends to look like this.

Object::Object() : foo(1), 
                            bar(2),
                            baz(3) {
    stuff();
}
Glomek
I guess that foo, bar, and baz are meant to be aligned.
Svante
This is how I would do it (bar and baz should be aligned with foo though).
Nick Presta
Yes, foo, bar, and baz are meant to be aligned. Fixed.
Glomek
A: 

I put the opening brace on a new line. The result looks mostly like the 2nd example, but without the indentation of the initializers.

That said, as this seems to be the part of C++ that has the widest range of layouts in practice, personally I've gone back to initializing variables in the constructor (where still efficient) in order to avoid offending anybody.

brone
Yeah me too. Also to avoid offending myself hehe.
I prefer that any simple initialization take place outside the constructor body. Save the body for code.
jmucchiello
A: 

I am admitted inconsistent globally in lots of things I do, meaning I do not have one global brace method, instead each language construct may have its own brace method which I use consistently.

 function()
 {
 }

 control-flow () {
 } else {
 }

 type name {
 };

As a constructor is a function...

 foo::foo()
  : bar(1)
  , baz(2)
 {
 }
jmucchiello
A: 

Here is my style:

Object::Object()
        : foo(1),
          bar(2) {

    // First line here.
}

It remains consistent with my bracket style, but also establishes a visual separation between the initializers and the code.

Jeff M