Lines of Code (LOC)
About
Lines of Code (LOC) is one of the oldest and simplest software metrics. It measures the size of a program based on the number of lines in its source code. LOC is often used as a rough indicator of:
Codebase size
Development effort estimation
Progress tracking over time
However, LOC does not directly measure code quality—a larger codebase does not necessarily mean better functionality, and fewer lines are not always better if they reduce clarity.
LOC is most useful when:
Comparing modules within the same codebase
Tracking size changes over time
Supporting other metrics such as Maintainability Index or productivity analysis
Types of LOC
1. Physical LOC (PLOC)
Counts every non‑blank, non‑comment line in a file.
Example: A function with 50 actual lines of code (excluding comments) has PLOC = 50.
Pros: Easy to calculate and standardize.
Cons: Sensitive to formatting (e.g., breaking a statement across lines).
2. Logical LOC (LLOC)
Counts statements rather than physical lines.
Example:
if (x > 0) { y = 1; z = 2; }
PLOC = 3 (if, y=1, z=2 are on separate lines)
LLOC = 3 (three statements, regardless of formatting)
Pros: More accurate representation of actual logic.
Cons: Harder to measure without tools.
3. Comment LOC (CLOC)
Measures only comment lines to track documentation coverage.
Useful for ensuring maintainable and well‑documented code.
LOC Interpretation Guidelines for Java
Scope
Good / Maintainable
Warning Zone
Refactor Alert
Notes
Method
≤ 20 LOC
21–50 LOC
> 50 LOC
Keep methods small and focused; apply Single Responsibility Principle.
Class
≤ 200 LOC
201–500 LOC
> 500 LOC
Consider splitting large classes; watch for “God Objects.”
Java File
≤ 500 LOC
501–1000 LOC
> 1000 LOC
Large files reduce readability and make merging harder.
Project Module
Context‑dependent
—
—
Compare with similar modules in our project for trends.
If using SonarQube, Checkstyle, or PMD, configure per‑method and per‑class LOC thresholds rather than a single project‑wide limit. This catches problems early without penalizing naturally verbose but well‑structured code.
Pros
Easy to Calculate
Simple to measure with basic tools or scripts.
Useful for Trend Tracking
Helps monitor codebase growth or shrinkage over time.
Supports Effort Estimation
When combined with historical data, can help estimate development effort.
Integration with Other Metrics
Works well as an input to metrics like Maintainability Index.
Cons
Not a Quality Metric
More lines do not mean better quality, and fewer lines do not always mean better design.
Language & Style Dependent
Formatting and programming language can skew LOC counts.
Encourages Wrong Behavior
Developers might write unnecessarily verbose code to inflate LOC counts.
Misleading Across Projects
Cannot directly compare LOC between different programming languages or paradigms.
Best Practices for Using LOC
Use LOC as a Supporting Metric, Not a Sole Indicator
Combine it with complexity, maintainability, and defect metrics for better insights.
Track Changes Over Time
Monitor growth to detect potential scope creep or over‑engineering.
Normalize by Language and Context
Avoid comparing LOC directly between projects in different languages.
Exclude Auto‑Generated Code
Generated files (e.g., OpenAPI stubs) can skew LOC statistics.
Include Comment LOC Tracking
Use CLOC to ensure adequate documentation coverage.
Last updated