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

后端 未结 12 1426
北海茫月
北海茫月 2021-02-04 01:02

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

相关标签:
12条回答
  • 2021-02-04 01:18

    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.

    0 讨论(0)
  • 2021-02-04 01:19

    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!

    0 讨论(0)
  • 2021-02-04 01:21

    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.

    0 讨论(0)
  • 2021-02-04 01:22

    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.

    0 讨论(0)
  • 2021-02-04 01:25

    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.

    0 讨论(0)
  • 2021-02-04 01:31

    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/

    0 讨论(0)
提交回复
热议问题