tags:

views:

157

answers:

4

I'm wondering in regards to the guideline stating that classes should have around 7 variables +-2, are class variables (class constants) included in this?

Ex:

class Foo
{
    static const int SOME_THING;
    static const double SOME_OTHER;
    static const int BLAH;

    int m_ThisVariable;
    double m_ThatVariable;
    string m_SomeString;
public:
    //....
};

Would you consider the above to count as 3 or 6 in regards to the 7 +- 2 rule?

+10  A: 

Anyone who honestly thinks that you can arbitrarily define how many member variables a class should have has not written a lot of code or are extremely arrogant. I know it just a guideline, but honestly, if the class is well defined, conforms to the general OOP guidelines of single responsibility, and is easy to maintain, you should just spend your time solving real problems.

BTW, I realize that this is not an actual answer, so let the downvoting begin. I just had to vent :)

EDIT: Just did a little searching and found that this 'guideline' comes from the fact that humans have trouble remembering sequences of information with more than five or six discrete data points. Well, that's nice, and it is something to remember (especially when designing user interfaces), but in practice you cannot design your code this way. Do what makes sense and makes your life easier (maintenance considerations being part of that decision).

Ed Swangren
I understand. It is just useful to have that guideline as a warning that you may want to start splitting your clas up.
Anonymous
But that's what I have a problem with. Some classes contain many many member variables and could not be split into multiple coherent classes (and should not be). I just don't think it is a useful guideline.
Ed Swangren
Wow, I completely thought that I was going to get hammered on this one :)
Ed Swangren
I think it is a useful guideline, because that's just what it is, a guideline. If you can't figure out when your domain-specific issues warrant not following a guideline, then that's the issue, not the guideline itself.
phoebus
But I am saying that it is not a useful guideline in any way. At all. Ever.
Ed Swangren
I agree. It can be useful to count the number of public members - more than a handful implies that the class might be trying to do more than one thing. As for private members, there must be enough to do what the class does, and no more. This "7 +/- 2" nonsense sounds like an attempt to replace the fundamental rule: Use Your Brain.
Mike Seymour
AUIU it's 7+-2 CHUNKS of information, not necessarily items. That's why most people remember phone numbers as chunks of 2-3 digits at a time, it's easier. I'm not sure I see the value of using this to limit class members though, the class is already a logical "chunk", with probably only a few "subchunks", at least it is if you've designed it right.
Andy J Buchanan
I would hope that professional programmers are capable of deciding when a class needs to be refactored without counting the number of private variables.
Ed Swangren
It also should concern *short-term* memory. With *learning* you can memorize more. - One probably uses short-term memory for quick reading of code. I'm not a pro, but when I come back to my old projects, I generally don't look up and memorize members of a class at all. What they mean is clear from the name, and I can be 99.8% sure an interesting variable is not a global :) so generally I look things up only on a need-to-know basis.
UncleBens
A: 

Aside from the fact that the number of variables shouldn't arbitrarily be set at a maximum number, I would argue that what is important is considering groups.

As such, I would consider static variables and non-static variables two separate groups (this is visually rendered in your code example as they are separated by a blank line). If they were all grouped together, then I'd think they count as one group.

I don't know however that this analysis has any value whatsoever, as I agree with Ed completely.

JRL
On the other hand, if you can split your class members into groups, that's often a good sign that you should split the class.
Kristopher Johnson
Not if the split is by accessibility, but even so, you are talking about logical groupings, i.e., someone is using their *brain*. That is usually a good thing, using arbitrary numbers is not.
Ed Swangren
A: 
Crashworks
A: 

I'm pretty sure constants shouldn't be counted. Most classes won't have many (any?), anyway. If your class does have a large number of constants, you probably ought to move them out into their own class, but one or two here and there aren't going to make any difference.

I know everybody is jumping in on the "this is crazy" side of this argument, so I'll just mention that I think it's not a totally unreasonable rule-of-thumb. In that respect, it's like the "no function longer than a single screen-full in the editor". Violating the rule just means you ought to take a careful look at the code and make sure it's not getting more-complex than necessary.

Mark Bessey