What Are Lines of Code?
Lines of code (LOC) is a software metric used to measure the size of a computer program by counting the number of lines in its source code. This quantitative measure falls under the broader umbrella of Software Engineering Metrics and has historically been applied in various contexts, including cost estimation, project tracking, and even attempts at gauging programmer productivity. While seemingly straightforward, the utility and accuracy of lines of code as a reliable indicator of software complexity or value are widely debated among professionals. LOC typically refers to all lines in a program, excluding comments and blank lines, but variations exist that may include these elements or only count executable statements.22
History and Origin
The concept of measuring software size by counting lines of code emerged early in the history of software development. As software projects grew in scale and complexity, a need arose for metrics to estimate effort, track progress, and compare projects. In the absence of more sophisticated methodologies, lines of code became a convenient, albeit simplistic, measure. Its widespread adoption was partly due to its ease of calculation. Despite its problems, this metric was often considered a starting point for software estimation, with some experts noting its utility in the absence of better alternatives.21 Jeff Atwood, in a 2006 piece on Coding Horror, discussed the limitations of lines of code, echoing the sentiment that while imperfect, it was often the "workhorse" technique for measuring project size.20
Key Takeaways
- Lines of code (LOC) is a direct measure of the physical size of a software program's source code.
- It is often used in preliminary cost estimation and project sizing in software development.
- Critics argue that LOC is a poor measure of productivity metrics or software quality.
- The metric can be easily manipulated and does not account for code efficiency, complexity, or business value.
- Variations exist, such as Logical Lines of Code (LLOC), which aim to refine the count by focusing on executable statements.
Formula and Calculation
The calculation of Lines of Code (LOC) is generally a simple count, though its precise definition can vary. The two most common methods are:
- Physical LOC: This counts every line of text in the source code file, including comments, blank lines, and formatting.
- Logical LOC (LLOC): This attempts to count only executable statements and declarations, ignoring comments and blank lines. It is considered a more refined measure as it focuses on the actual programming work.19
There is no complex mathematical formula for LOC itself; it is a direct enumeration. For instance, in a simple C++ program: