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)
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.
20 is a lot of parameters, it probably deserves to be broken up. “God classes” are 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 IOand// 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.
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.