CodeQL queries 1.25
Skip to end of metadata
Go to start of metadata

Name: Lines of code in files

Description: Measures the number of lines of code in each file, ignoring lines that contain only comments or whitespace.

ID: go/lines-of-code-in-files

Kind: metric

Precision: very-high

Query: FLinesOfCode.ql
/**
 * @name Lines of code in files
 * @description Measures the number of lines of code in each file, ignoring lines that
 *              contain only comments or whitespace.
 * @kind metric
 * @treemap.warnOn highValues
 * @metricType file
 * @metricAggregate avg sum max
 * @precision very-high
 * @id go/lines-of-code-in-files
 * @tags maintainability
 */

import go

from File f, int n
where n = f.getNumberOfLinesOfCode()
select f, n order by n desc

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.