开发者

Are class variables included in the 7 +- 2 guideline?

I'm wondering in regards to the guideline stating that classes should have around 7 variables开发者_StackOverflow +-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?


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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜