问题
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?
回答1:
"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.
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 = 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 Clean Code: A Handbook of Agile Software Craftsmanship
回答2:
Generally speaking I would stick, as best possible, to a method not exceeding one page. So say one method is 50 lines + comments, a big method BTW, so it would mean you would have maybe 6 methods in 500 lines. I suspect you have methods exceeding a page, which is making your classes very large.
As others have mentioned, code quality suffers the larger your classes get. I would see how much of your code can go into an abstract class, or smaller classes that are reference as part of these others classes.
That being said, sometimes its not easy to have a small class. But if all your classes are large, you may not be taking advantage of abstract classes, aggregation, etc.
回答3:
I think the size of a class, based on the number of lines of code, is a vague indicator on whether it should be broken down.
However, we should always try to write minimal number of classes and methods. This is because small stuff is easier to understand – but if there is too many (though perfectly understandable) pieces, you won’t be able to see the whole picture.
Also, there are constraints on the JVM that you need to be aware of, such as the method size as explained in this article.
You should remove methods that you no longer need (or which may have been added only out of fear). If code is not used, it is of less than zero value. A class should have one and only one clearly defined responsibility. If your class is/has/does several different things, what are you going to call the class?
You need to find the right balance.
来源:https://stackoverflow.com/questions/16351566/what-is-the-recommended-size-of-a-class