Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm maintaining a Java class that's 40K lines long.. problem?

This may be a subjective question leading to deletion but I would really like some feedback.

Recently, I moved to another very large enterprise project where I work as a developer. I was aghast to find most classes in the project are anywhere from 8K to 50K lines long with methods that are 1K to 8K lines long. It's mostly business logic dealing with DB tables and data management, full of conditional statements to handle the use cases.

Are classes this large common in large enterprise systems? I realize without looking at the code it's hard to make a determination, but have you ever worked on a system with classes this large?

like image 806
Billworth Vandory Avatar asked Dec 26 '10 20:12

Billworth Vandory


Video Answer


4 Answers

Here are the ten largest class in the JDK 6 by line count of 7209 .java files. These classes include significant amount of comments which could be longer than the code.

4495 ./javax/sql/rowset/BaseRowSet.java
4649 ./java/awt/Container.java
5025 ./javax/swing/text/JTextComponent.java
5246 ./java/util/regex/Pattern.java
5316 ./javax/swing/JTree.java
5469 ./java/lang/Character.java
5473 ./javax/swing/JComponent.java
9063 ./com/sun/corba/se/impl/logging/ORBUtilSystemException.java
9595 ./javax/swing/JTable.java
9982 ./java/awt/Component.java

I would agree one printed page is long enough for a method. There really should not be a need for classes over 10K lines long IMHO.

like image 98
Peter Lawrey Avatar answered Oct 18 '22 11:10

Peter Lawrey


This is definitely not right. A method should not contain more code than sufficient for a single unit of work. A class should not contain more methods than the ones related to the state of the class' instance.

This is too much like God Object anti-pattern. I would personally drop the project and look for another.

like image 33
BalusC Avatar answered Oct 18 '22 09:10

BalusC


Without looking at the code, it actually remains quite easy to make a determination. Never should a class be 40K lines, and never should a method be even 1K. Typically, if I can't print out a method on a piece of paper and see both the beginning and end brackets, I find a way to split it up.

Might I ask, are they using OOP principles at all, or are they trying to use Java more as a functional or procedural language? I can't imagine a truly OOP project having a 40K line class.

like image 31
Serplat Avatar answered Oct 18 '22 11:10

Serplat


Oh, I think this a terrible sign, and I don't have to look at the code to say so. Sounds like a massive refactoring effort is needed.

Let me guess - you have no unit tests for the system as written, either. You have my sympathy.

like image 26
duffymo Avatar answered Oct 18 '22 10:10

duffymo