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条回答
Deceive 欺骗
2楼-- · 2019-03-14 13:26

As a young programmer I still remember my teacher telling us to break up big functions and work on a good OO design before writing code.

So unless there is a REALLY good reason in your design to impose 40k lines (which I strongly doubt) then you already have your answer : your class is too big.

I will quote my wife (who is chemist and doesn't program) : "40k lines of code, there is something really wrong!"

I've had friends take up projects in their companies that were really old, tossed from one programmer to the over and what we all agreed on is that a class that size simply means:

-patch and fix : people had to do minor changes here and there and didn't want to/ didn't have the time to do it corretly.

at runtime there might not be any problems with that code, everything works, but usually problems occure when you want to do any form of modifications:

  • it takes ages to find anything

  • when there is a bug you can't pin point it easily

...

In conclusion I would sit down rethink the oo design of your project and restructure (at least into 1k ~ 5k lines classes to start with), I know its annoying to do bu usually on the long run its better

查看更多
成全新的幸福
3楼-- · 2019-03-14 13:27

50K lines of code? I thought KLOC was a metric of project size, not file size. That's like our whole codebase (including tests).

I'm working with JavaScript, so it's not directly comparable, but we only have few files that are over 500 lines long - and these are the highly problematic ones.

查看更多
叼着烟拽天下
4楼-- · 2019-03-14 13:28

In 12 years of Java development I can honestly say that this is unusual.

In fact; I have never come across files or classes of that size in any language in over 25 years of development.

Crack out the refactoring tools!

查看更多
女痞
5楼-- · 2019-03-14 13:31

An alarm bell started going off when I read through this :

It's mostly business logic dealing with DB tables and data management, full of conditional statements to handle the use cases.

If this code isn't in the data layer and there is no abstraction with respect to how the database is accessed, something is wrong. I also have the feeling that some of these methods aren't directly related to the classes where they are found. The comment about conditional statements and use cases doesn't sound right either. I'll echo duffymo's comment that some refactoring would be needed.

查看更多
时光不老,我们不散
6楼-- · 2019-03-14 13:33

I think your aghastness is qualified :) I can't imagine that the program is properly OOPified. Classes are a bit tougher to classify, but methods are easy: 1 behavior per method (that's not a rule, but it should be). Behaviors can't possibly be even close 1k lines of code. At least, as far as my imagination will take me.

Classes, on the other hand, can represent many things but they should represent something. If it is difficult to tell what the class represents, then you have a problem.

Now, I half imagine that you are well aware of these concepts and I'm preaching to the choir. So, I'll just pretend like I didn't go off on a tangent there and answer your question directly:

Yes. Very unfortunately, it is remarkably common for large enterprise projects to have code that lazy. I have worked on projects almost as large (yours shoots anything I've seen out of the water) and my first tendency is to start breaking things up into logical components, particularly in places where I intend on making changes. I can't handle that kind spaghetti, it's too irritating.

查看更多
Luminary・发光体
7楼-- · 2019-03-14 13:36

In addition to software maintenance issues described in other answers, be careful of the technical limit that a compiled Java method may not exceed 64k bytes. (How many lines of code that will correspond to will depend on the lines themselves.)

http://www.databasesandlife.com/java-method-64k-limit/

查看更多
登录 后发表回答