I'm maintaining a Java class that's 40K li

2019-03-14 13:04发布

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?

12条回答
Bombasti
2楼-- · 2019-03-14 13:11

A small thing to pay attention to is the difference between lines, lines of code, and statements. If you analyze your project with e.g. Sonar you can easily see the difference between those.

Nevertheless, whatever the exact measure, 40k lines of business code is hideous.

In the business module of an enterprise application I develop, the highest number is 444 lines of code. This is for a rather large Service. Most Service classes are between 200 and 100 lines of code. Entities (model objects) are in our situation mostly between 40 and 100 loc.

In another part of this same application we have one class that is 1224 lines of code (2477 lines total, 706 statements). This class is almost universally hated within the team because of its size. It's perceived as bloated, complicated and doing way too much.

Now if an entire team thinks this about a class that's only 2477 lines total, this may give you some perspective about what kind of abomination a 40k lines class is.

查看更多
再贱就再见
3楼-- · 2019-03-14 13:15

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.

查看更多
三岁会撩人
4楼-- · 2019-03-14 13:19

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.

查看更多
冷血范
5楼-- · 2019-03-14 13:19

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.

查看更多
啃猪蹄的小仙女
6楼-- · 2019-03-14 13:21

I know some section of code needs breaking up when I have problems trying figure out what it does and how. Or if its larger than a screen-shot.

Typically I refactor it if it makes me feel bad. I don't like feeling bad :-(

I suspect the code got out of control because the management just wanted bugfixes/features done and nothing else, and bit by bit it slowly got worse.

Refactoring the code to make the programmers job easier was probably not very high on their list of priorities. The management always want their bugfixes/features yesterday :-(

Also a working program is obviously better than one thats broke, and breaking up code that large without unit-tests would end in disaster. So all the tests would have to be made before touching the code. Yet another reason management wouldn't allow it.

查看更多
Deceive 欺骗
7楼-- · 2019-03-14 13:26

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.

查看更多
登录 后发表回答