CodeQL documentation

Lines of code in files

ID: java/lines-of-code-in-files
Kind: treemap
Severity: 
Precision: very-high
Tags:
   - maintainability
   - complexity

Click to see the query in the CodeQL repository

There are a number of problems associated with a high number of lines of code:

  • It can be difficult to understand and maintain, even with good tool support.

  • It increases the likelihood of multiple developers needing to work on the same file at once, and it therefore increases the likelihood of merge conflicts.

  • It may increase network traffic if you use a version control system that requires the whole file to be transmitted even for a tiny change.

  • It may arise as a result of bundling many unrelated things into the same file, and so it can indicate weak code organization.

Recommendation

The solution depends on the reason for the high number of lines:

  • If the file’s main class is too large, you should refactor it into smaller classes, for example by using the ‘Extract Class’ refactoring from [Fowler].

  • If the file’s main class contains many nested classes, you should move the nested classes to their own files (in a subsidiary package, where appropriate).

  • If the file contains multiple non-public classes in addition to its main class, you should move them into separate files. This is particularly important if they are logically unrelated to the file’s main class.

  • If the file has been automatically generated by a tool, no changes are required because the file will not be maintained by a programmer.

References

  • M. Fowler, Refactoring. Addison-Wesley, 1999.