views:

719

answers:

3

When getting metrics from a project, if you want to evaluate if a class is way too big, apart from other things, you can look at the number of lines.

For Java in particular, what would you say a recommended number would be?

I'm looking for a number to be used as part of my continuous integration server checks.

Java code conventions (from 1997) recommend a maximum of 2000 lines. http://java.sun.com/docs/codeconv

Related:

http://stackoverflow.com/questions/374262/is-there-a-recommended-number-of-lines-of-code-per-file

http://stackoverflow.com/questions/107855/is-there-any-no-of-lines-code-limit-for-a-java-class

http://stackoverflow.com/questions/1248873/lines-of-a-class-in-java

+12  A: 

Number of lines is not a good metric. A better rule would be to follow the Single Responsibility Principle. ie. A class should only do one thing. Try to identify code smells. Does your class have feature envy or does it contain methods that have too many parameters? Does your class violate the Law of Demeter? These are just some code smells to look for. When you encounter code smells, refactor and you'll find your classes often naturally shrink in size or get broken up into multiple smaller classes.

Asaph
Still, when you work with other programmers they might not do all these checks. I want to have a number as a failsafe, if every other check fails.
Iker Jimenez
@Iker Jimenez: It's understandable to want a simple metric that can be easily reported to management. But measuring lines of code is highly gameable. For example, a programmer who wants fewer lines of code can simply start combining separate lines together to give the illusion of fewer lines. In the end maintainability suffers.
Asaph
I like the lines-per-method metric more - it can be helpful even for experienced developers - at some point it says "split me in two methods"
Bozho
@Bozho: Good point. In his book Clean Code, Robert C. Martin advises programmers to aim for 5 lines per method or less. But it's just a guideline and exceptions are occasionally made.
Asaph
Keep in mind that lines-per-class is something that can be checked with static analysis tools, e.g. PMD (that Bozho mentioned). Then the developer can decide whether to do something about it or not.
Eli Acherkan
@Asaph I'm still setting up the whole thing but I think that I'll stick in a value like 1000 for the moment and hope that other checks raise the alarms before reaching this constraint.
Iker Jimenez
@Iker Jimenez: I think maybe use the 1000 line cap as a way to flag a class as a candidate for code review. That seems pretty sane to me.
Asaph
+1  A: 

Take a look at this article

Also take a look at PMD's default values for lines per method and lines per class here

On the whole, it is very subjective. But it is true that observing such rules can make programmers think more of the quality of their code.

Bozho
A: 

Rather than focus on the lines-of-code metric, you might want to use a metric that attempts to measure the code's complexity -- such as cyclomatic complexity.

Steve Emmerson
Already doing it, but thanks anyway.
Iker Jimenez