views:

103

answers:

3

How do you deal with them? I have some classes (usually classes that hold stats etc.) with some 20+ variable members, and the initialization lists end up very long, extending beyond the page width if I don't manually wrap around. Do you try and break down such classes or do you deal with this in some other way?

It doesn't look very tidy, but sometimes I write the variables in the list on top of each other like so:

myConstructor(var1, var2, var3, ..., varN) :
 member1(var1),
 member2(var2),
 member3(var3),
 ...
 memberN(varN)
+1  A: 

extending beyond the page width

Well, first off, you should probably decide on a page width and stick to it. Use auto line wrapping from your editor, if you want. Reading code that's greater than your window size is really difficult, especially for your coworkers using vi or emacs from terminals. Pick a page width and stick to it- this means wrapping these initializer lists onto multiple (possibly many) lines.

Do you try and break down such classes?

20 is a lot of parameters, it probably deserves to be broken up. "God classes" and generally a code smell and indicate a refactoring is necessary.

It doesn't automatically mean you should break things up, there are always exceptions to guidelines. But, definitely consider it as an option.

When you declare them in the header file, do you (or could you) group them with comments? For example: // These next few parameters are for file IO and // These next parameters are for the widget, that will provide you a good template for which objects are looking to be abstracted.

Overall really large classes indicate a lot of complicated state, and complicated state tends to cause bugs. You should, like with functions, prefer to keep them small and focused.

{Your example code}

I think is quite readable and "tidy", although it will probably be pretty long list. As I mentioned, to combat this, I'd consider breaking it up into smaller classes.

Stephen
+2  A: 

Any class with twenty constructor arguments probably need to be refactored. I would consider using composition, breaking up big classes into smaller, independent pieces. Think about which of your constructor arguments are tightly related, and group those variables into their own class. You can then pass an instance of your new class to whichever classes need access to the data.

Evan Kroske
+3  A: 

the initialization lists end up very long, extending beyond the page width if I don't manually wrap around

One way is to refactor: for example, instead of passing in 4 primitive variables ("top", "left", "width", and "height"), just pass in one compound variable ("rectangle").

Alternatively, just to do with source code layout:

class Foo
{
    int m_a;
    int m_b;
    int m_c;
public:
    Foo(
        int a,
        int b,
        int c
        )
        : m_a(a)
        , m_b(b)
        , m_c(c)
    {
    }
};
ChrisW
I don't know why I've put their comma after each parameter but before each initializer.
ChrisW