← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Lines of Code (LOC) as a Metric

quality Beginner

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/

Added 15 Mar 2026
Edited 22 Mar 2026
Views 19
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping F 0 pings S 0 pings S 2 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S
No pings yet today
Amazonbot 7 Perplexity 2 Ahrefs 2 Majestic 1 ChatGPT 1 Unknown AI 1 Google 1
crawler 15
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

✓ schema.org compliant