建议的班级人数是多少? [英] What is the recommended size of a class?

查看:72
本文介绍了建议的班级人数是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

a一直在编写Java EE 6 Web应用程序,这实际上是我正在编写的第一个严肃的应用程序.我注意到我的课程多达500行至1000行,并且可能会越来越大.我不知道一个类应该有多大,或者根本不重要,但是如果它对应用程序性能产生负面影响,我不想继续写巨大的类.您对我有什么建议?

a have been writting a Java EE 6 web application which is really my first serious application I am coding. I have noted that my classes are as big as 500 lines to 1000 and could probably get bigger. I have no idea how big a class should be or if it matters at all but I don't want to continue writting gigantic classes if it is going to impact the applications performance negatively. what advice do you have for me?

推荐答案

类的第一条规则是小.第二类的规则是小. 应该小于那个."

"The first rule of classes is that they should be small. The second rule of classes is that they should be smaller than that."

班级的名称应描述班级履行的职责.实际上,命名 这可能是帮助确定班级人数的第一种方法.如果我们不能简明扼要 某个班级的名称,则可能太大.类名越歧义,就越 它可能承担了太多的责任.例如,包含黄鼠狼词的类名 像处理者或管理者或超级用户经常暗示不幸的是 责任.

The name of a class should describe what responsibilities it fulfills. In fact, naming is probably the first way of helping determine class size. If we cannot derive a concise name for a class, then it’s likely too large. The more ambiguous the class name, the more likely it has too many responsibilities. For example, class names including weasel words like Processor or Manager or Super often hint at unfortunate aggregation of responsibilities.

我们还应该能够用大约25个词写出该类的简短说明, 而不使用"if","and","or"或"but"一词.我们将如何描述 超级仪表板? "SuperDashboard提供对上次保存组件的组件的访问. 重点,它还使我们能够跟踪版本和内部版本号."第一个和"是 暗示SuperDashboard职责过多.

We should also be able to write a brief description of the class in about 25 words, without using the words "if," "and," "or," or "but." How would we describe the SuperDashboard? "The SuperDashboard provides access to the component that last held the focus, and it also allows us to track the version and build numbers." The first "and" is a hint that SuperDashboard has too many responsibilities.

我们希望我们的系统由 很多小班,而不是几个大班.每个小类都封装一个职责, 有一个改变的唯一原因,并与其他一些人合作以实现 所需的系统行为.

We want our systems to be composed of many small classes, not a few large ones. Each small class encapsulates a single responsibility, has a single reason to change, and collaborates with a few others to achieve the desired system behaviors.

COHESION =类应具有少量实例变量.一类的每种方法 应该操纵这些变量中的一个或多个.一般而言,变数较多的一种方法 操纵该方法对它的类更具凝聚力.一个类,其中每个变量是 每种方法所使用的最大内聚力. 通常,创建这样的最大内聚力既不建议也不可行 类;另一方面,我们希望凝聚力很高.凝聚力高时 表示该类的方法和变量是相互依赖的,并作为一个整体挂在一起 逻辑整体.

COHESION = Classes should have a small number of instance variables. Each of the methods of a class should manipulate one or more of those variables. In general the more variables a method manipulates the more cohesive that method is to its class. A class in which each variable is used by each method is maximally cohesive. In general it is neither advisable nor possible to create such maximally cohesive classes; on the other hand, we would like cohesion to be high. When cohesion is high, it means that the methods and variables of the class are co-dependent and hang together as a logical whole.

-from 清洁代码:敏捷手册 软件工艺

-from Clean Code: A Handbook of Agile Software Craftsmanship

这篇关于建议的班级人数是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆