CodeQL documentation

Lines of code in files

ID: go/lines-of-code-in-files
Kind: metric
Severity: 
Precision: very-high
Tags:
   - maintainability

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 contains one or more very large functions, you should decompose them into smaller functions by means of the Extract Function refactoring.

  • If the file contains many smaller functions, you should try to split up the file into multiple smaller files.

  • 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.