← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

Dead Code Elimination

Compiler PHP 7.4+ Intermediate
debt(d3/e3/b3/t5)
d3 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'default linter catches' (d3). PHPStan (listed in detection_hints.tools) catches dead code at level 2+, Psalm and Rector also detect unreachable branches and unused functions. These are standard static analysis tools in the PHP ecosystem that flag dead code patterns automatically.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix indicates enabling PHPStan dead code detection and using named exports — these are configuration-level changes. Removing dead code itself is typically deleting unreachable branches or unused functions, which is straightforward but may touch multiple files when cleaning up an entire codebase.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). Dead code imposes cognitive load on maintainers who must parse through irrelevant branches, but it's localized — each piece of dead code affects only the file/component it lives in. It doesn't create architectural gravity or cross-cutting dependencies; it's clutter that accumulates but can be incrementally removed.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception field explicitly states developers think dead code is just commented-out code, missing that it includes unreachable branches after return/throw, always-true conditions, and defined-but-never-called functions. This is a documented gotcha — developers learn it eventually but initially underestimate the scope of what constitutes dead code.

About DEBT scoring →

Also Known As

DCE tree shaking unreachable code removal dead code removal

TL;DR

A compiler and static analysis optimisation that identifies and removes code that can never be executed or whose result is never used.

Explanation

Dead code elimination (DCE) operates at two levels. At the compiler level, it removes unreachable branches (after `return`, inside impossible conditions), unused variables whose computed values are never read, and pure expressions whose results are discarded. Modern JS bundlers (Webpack, Rollup, esbuild) implement tree-shaking — a module-level form of DCE that removes entire exported functions never imported by the rest of the bundle, shrinking JavaScript payloads significantly. In PHP, OPcache performs some DCE at the opcode level. Static analysers like PHPStan and Psalm detect dead code as a quality signal: unreachable code after a `return`, conditions that are always true/false, and methods that are never called. From a developer perspective, dead code increases cognitive load, misleads future readers, and can mask bugs (code left commented out or bypassed rather than deleted).

Common Misconception

Dead code is just commented-out code — dead code also includes unreachable branches after `return`/`throw`, conditions that are always true or false, and functions that are defined but never called anywhere in the codebase.

Why It Matters

In PHP, dead code inflates cognitive complexity and hides intent. In JavaScript, unreferenced exports bloat bundle size — tree shaking can cut bundles by 30–60% on large codebases. Static analysis catching dead branches often reveals logic errors that were silently bypassed.

Common Mistakes

  • Leaving `else` branches after a `return` in the `if` — the else is dead; PHPStan flags this.
  • Defining constants or configuration values that are never read — clutters the codebase and can mislead.
  • Using `export default` for everything in JavaScript — prevents tree shaking; named exports are required for bundlers to eliminate unused code.
  • Keeping bypass flags like `if (false) { // old logic }` instead of deleting — dead code that silently rots and confuses readers.

Code Examples

💡 Note
Named JS exports let bundlers statically analyse import graphs and remove `unusedFn` from the final bundle entirely.
✗ Vulnerable
// PHP dead code patterns:
function process(int $value): string {
    if ($value > 0) {
        return 'positive';
    } else {
        return 'non-positive'; // else is dead after return — always executes
    }
    echo 'never reached'; // dead — after both returns
}

// JS: default export blocks tree shaking
export default {
    usedFn() { /* used */ },
    unusedFn() { /* bundler cannot eliminate this */ }
};
✓ Fixed
// PHP: remove dead else
function process(int $value): string {
    if ($value > 0) {
        return 'positive';
    }
    return 'non-positive'; // clear — no dead else
}

// JS: named exports enable tree shaking
export function usedFn() { /* used — bundler keeps */ }
export function unusedFn() { /* never imported — bundler eliminates */ }

Added 24 Mar 2026
Views 88
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 2 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 1 ping S 1 ping M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 1 ping F
SEMrush 1
No pings yesterday
Amazonbot 17 Perplexity 9 Scrapy 7 Ahrefs 6 SEMrush 6 ChatGPT 5 Unknown AI 5 Google 3 Bing 2 PetalBot 2 Meta AI 1 Qwen 1 Twitter/X 1 Brave Search 1 Applebot 1
crawler 61 crawler_json 5 pre-tracking 1
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
Compiler general A compiler is a program that translates your entire source code into machine-readable instructions before the program runs, catching errors upfront and producing an executable file.

Understanding compilation explains why some errors appear before your code runs, why compiled programs are often faster, and how tools like TypeScript catch bugs at build time rather than in production.

💡 When you see an error before your code runs, that's the compiler helping you—read the line number and fix the syntax first.

Ask Codex about Compiler →
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Enable PHPStan dead code detection (level 2+) and use named JS exports to allow bundler tree shaking
📦 Applies To
PHP 7.4+ web cli
🔗 Prerequisites
🔍 Detection Hints
Code after return/throw/continue; else branch following a return; functions defined but never referenced
Auto-detectable: ✓ Yes phpstan psalm rector
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: Function


✓ schema.org compliant