类变量是否包含在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.

推荐文章

我可以在Guice的Module.configure()中使用已经绑定的实例吗?

我可以在Guice的Module.configure()中使用已经绑定的实例吗?

推荐文章

C++中的RTI开销

C++中的RTI开销

推荐文章

表视图中嵌入的图像URL-如何立即显示页面

表视图中嵌入的图像URL-如何立即显示页面

推荐文章

一种对url进行分组的方法

一种对url进行分组的方法

推荐文章

具有web.config文件的IIS上的Codeigniter 2

具有web.config文件的IIS上的Codeigniter 2

推荐文章

LogicalBinaryExpression到MemberExpression

LogicalBinaryExpression到MemberExpression

推荐文章

替换文件最后一行的有效方法?

替换文件最后一行的有效方法?

推荐文章

如何将字节[]中的HTML保存为图像???c类#

如何将字节[]中的HTML保存为图像???c类#

推荐文章

哪里可以得到libc_p.so源代码?

哪里可以得到libc_p.so源代码?

推荐文章

验证表中的数据

验证表中的数据

推荐文章

Symfony 2认证(条令)实体

Symfony 2认证(条令)实体

推荐文章

用每种语言创建计划任务

用每种语言创建计划任务

推荐文章

数学中的符号条件期望

数学中的符号条件期望

推荐文章

确定元素在文档上的绝对位置

确定元素在文档上的绝对位置

推荐文章

从C访问经过SPNEGO身份验证的Web服务#

从C访问经过SPNEGO身份验证的Web服务#

推荐文章

如果验证失败,是否有任何简单的方法可以添加错误类以使用Zend framework形成字段?

如果验证失败,是否有任何简单的方法可以添加错误类以使用Zend framework形成字段?