类变量是否包含在7+2准则中?

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?

#0

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).

#1

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.

#2

By the way, if you want a convenient means of grouping variables together in the IDE without having to actually put them into classes, MSVC supports the #pragma region directive. That just lumps some lines of code together into regions that can be collapsed or expanded by clicking the little "+" icon to the left — it has no effect on the compiled result, it's just markup for the code editor.

#3

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.