Dead Code
debt(d3/e1/b3/t5)
Closest to 'default linter catches the common case' (d3). PHPStan and Psalm, listed in detection_hints.tools, catch unreachable code after return/throw and unused private methods at default or moderate strictness levels. While not always enabled by default in projects, these are standard PHP static analysis tools that flag dead code patterns readily.
Closest to 'one-line patch or single-call swap' (e1). The quick_fix is explicit: 'Delete it.' Removing dead code is pure deletion—no replacement logic needed, no dependencies to update. Git preserves history if rollback is ever needed.
Closest to 'localised tax' (b3). Dead code imposes cognitive load on maintainers who must read and reason about code that does nothing. However, since it doesn't execute, it doesn't create runtime dependencies or architectural constraints. The burden is real but contained—each piece of dead code affects comprehension in its local area rather than shaping system architecture.
Closest to 'notable trap' (t5). The misconception field directly states the trap: developers believe 'dead code is harmless since it never runs.' This is a documented gotcha that experienced developers eventually learn—dead code misleads maintainers, inflates cognitive load, can contain old vulnerabilities, and may be accidentally re-enabled during merges. The intuition that non-executing code is cost-free is wrong but not catastrophically so.
Also Known As
TL;DR
Explanation
Dead code increases the cognitive load of reading code without providing any value. It can arise from if(false) conditions left from debugging, code below an unconditional return, or logic that was superseded but never removed. Dead code is confusing because readers try to understand why it's there. Worse, if a bug exists in dead code, it may be noticed and "fixed" — causing it to become live and introduce a regression. Remove dead code; source control preserves the history.
Common Misconception
Why It Matters
Common Mistakes
- Leaving commented-out code in version control — use git; if it's commented out, delete it.
- Unreachable code after return/throw that static analysis would catch if it were run.
- Keeping old API endpoints 'just in case' without deprecation notices or removal timelines.
- Private methods that are never called — IDEs flag these but warnings are often ignored.
Code Examples
function processOrder(Order $o): void {
if ($o->isPaid()) {
return;
}
// This block can never run for a paid order:
$this->chargeCard($o); // dead
$this->sendReceipt($o); // dead
}
function processOrder(Order $o): void {
if ($o->isPaid()) {
return;
}
$this->chargeCard($o);
$this->sendReceipt($o);
}
// Or restructure so all paths are live:
function processOrder(Order $o): void {
if (!$o->isPaid()) {
$this->chargeCard($o);
$this->sendReceipt($o);
}
}