Lines of Code (LOC) as a Metric
Also Known As
LOC
SLOC
source lines of code
TL;DR
The simplest size metric — useful for normalising other metrics and tracking growth, but a poor quality indicator when used in isolation.
Explanation
Lines of Code has three variants: physical LOC (all lines), source LOC (non-blank, non-comment), and logical LOC (statements). LOC is useful for normalising defect density (defects per KLOC) and tracking codebase growth trends. As a standalone quality metric it is misleading — a 500-line function and ten 50-line functions share the same LOC but differ enormously in maintainability. LOC is best used alongside complexity metrics (cyclomatic, cognitive) rather than as a target. Setting LOC limits per file in CI is a lightweight smell detector that flags candidates for refactoring review.
Common Misconception
✗ Fewer lines of code always means better code. Extremely compact code can be harder to read and maintain than slightly more verbose but explicit code. LOC is a weak proxy for complexity — use it alongside other metrics, never alone.
Why It Matters
Lines of code is the simplest size metric — while not a quality metric by itself, abnormal counts (very high or very low) reliably indicate areas worth examining for complexity or laziness.
Common Mistakes
- Treating LoC as a productivity metric — incentivising more lines produces bloated, verbose code.
- Using LoC as the only size metric — a 50-line function with 15 branches is more problematic than a 200-line class with simple sequential logic.
- Reducing LoC by compressing multiple statements onto one line — not an improvement.
- Not normalising LoC by language — PHP and Java will naturally produce different counts for equivalent logic.
Code Examples
✗ Vulnerable
// 'Reducing' LoC by unreadable compression — worse, not better:
function p($u){return$u->a&&$u->v&&!$u->b?'active':($u->b?'banned':'inactive');}
// Low line count, zero readability — meaningful LoC reduction requires refactoring, not compression
✓ Fixed
// Lines of Code (LOC) is a simple but blunt metric
// Logical LOC (non-blank, non-comment) is more meaningful
// Tools to measure PHP LOC:
$ phploc src/
// Reports: LOC, LLOC (logical), CLOC (comment), NCLOC, methods, classes
// Rough guidelines (not strict rules):
// Method: < 20 LLOC
// Class: < 200 LLOC
// File: < 400 LLOC
// LOC alone doesn't indicate quality — pair with:
// - Cyclomatic complexity
// - Test coverage
// - Maintainability Index
// SonarQube and PHPMetrics produce LOC dashboards:
$ phpmetrics --report-html=report/ src/
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
19
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
Amazonbot 7
Perplexity 2
Ahrefs 2
Majestic 1
ChatGPT 1
Unknown AI 1
Google 1
Also referenced
How they use it
crawler 15
Related categories
⚡
DEV INTEL
Tools & Severity
🔵 Info
⚙ Fix effort: Low
⚡ Quick Fix
Use LOC as a starting indicator only — high LOC signals classes worth examining, but quality is better measured by cyclomatic complexity, cohesion, and test coverage
📦 Applies To
any
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
File >500 lines; method >50 lines; class >300 lines; no LOC baseline to track growth over time
Auto-detectable:
✓ Yes
phploc
phpmetrics
sonarqube
cloc
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Medium
✗ Manual fix
Fix: Medium
Context: File
Tests: Update