DispensablesLow

Dead Code: What It Costs and How to Fix It

Code that is never executed: unreachable branches, unused variables, commented-out blocks, and orphaned functions.

Annual Cost$600 - $5k
Severity
2/5
CategoryDispensables
Detection4 tools

What It Is

Dead Code is any code that is never executed at runtime. This includes unreachable branches after an early return, unused functions and classes, commented-out code blocks left 'just in case,' and variables that are assigned but never read. Dead Code accumulates gradually as features are removed, requirements change, and refactoring is incomplete. It is the easiest smell to detect (most static analysis tools catch it automatically) and the easiest to fix (delete it), yet it persists in most codebases because developers are afraid to remove code they did not write.

Threshold: Any commented-out code block longer than 2 lines should be deleted. Any function with zero callers (verified by static analysis) should be deleted. Trust version control.

Why It Costs Money

1

Reading burden is the primary cost. Every developer who navigates through a file with dead code must spend cognitive effort determining whether the code is live or dead. Commented-out blocks are particularly expensive because they suggest someone wanted to keep the code, making developers hesitant to delete it.

2

False positives in search results. When searching for usages of a function or variable, dead code appears in results, slowing down investigation. Developers must check each result to determine if it is live code or dead.

3

Build and test time inflates. Dead code that compiles but is unreachable still adds to compilation time, test coverage gaps, and code complexity metrics.

Specific Cost Mechanisms

  • Reading overhead: 5-10 minutes per developer per week wasted reading dead code
  • Search result noise: dead code in search results adds 10-20% overhead to code investigation tasks
  • False sense of test coverage: unreachable code counted in coverage metrics obscures actual coverage

Estimated Annual Cost

Cost per instance by team size and codebase size. Based on $120,000 average developer salary. See full methodology.

Team SizeSmall (<50k LOC)Medium (50k-200k)Large (200k+)
3 devs$600$1,500$3,000
5 devs$1,000$2,500$5,000
10 devs$1,500$3,750$5,000
20 devs$2,000$5,000$5,000

How to Detect It

Specific rules and thresholds for automated detection. See full tool comparison.

SonarQube
squid:S1068 / squid:S1144 / squid:S125

Unused fields, unused private methods, commented-out code

CodeClimate
unused-code

Detects unreferenced functions and variables

ESLint
no-unused-vars / no-unreachable

Catches unused variables and unreachable code after returns

TypeScript
noUnusedLocals / noUnusedParameters

Compiler flags for unused declarations

Refactoring Patterns

Proven techniques to eliminate this smell. See all refactoring patterns.

Delete and trust version control

Always. Dead code can always be retrieved from git if needed.

Effort: 5-15 minutes per block
Impact: Immediate reduction in reading burden and search noise

Enable strict compiler flags

Setting up a codebase for the first time or during a cleanup sprint

Effort: 1-2 hours to fix all warnings
Impact: Prevents future dead code accumulation

Run coverage analysis

Identifying dead code that is not syntactically obvious

Effort: 2-4 hours for initial analysis
Impact: Finds dead code that static analysis misses

Related Smells