{
    "slug": "dead_code",
    "term": "Dead Code",
    "category": "quality",
    "difficulty": "beginner",
    "short": "Code that can never be executed — unreachable after a return, or inside a condition that is always false.",
    "long": "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.",
    "aliases": [
        "unreachable code",
        "unused code",
        "zombie code"
    ],
    "tags": [
        "quality",
        "refactoring",
        "static-analysis"
    ],
    "misconception": "Dead code is harmless since it never runs. Dead code misleads maintainers into thinking functionality exists, inflates cognitive load, can contain old vulnerabilities, and is occasionally accidentally re-enabled during merges.",
    "why_it_matters": "Dead code misleads maintainers into believing functionality exists, adds to cognitive load, and can mask live bugs when execution paths are more complex than they appear.",
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "duplicate_code",
        "code_smell"
    ],
    "prerequisites": [
        "refactoring",
        "static_analysis"
    ],
    "refs": [
        "https://refactoring.guru/smells/dead-code"
    ],
    "bad_code": "function processOrder(Order $o): void {\n    if ($o->isPaid()) {\n        return;\n    }\n    // This block can never run for a paid order:\n    $this->chargeCard($o);  // dead\n    $this->sendReceipt($o); // dead\n}",
    "good_code": "function processOrder(Order $o): void {\n    if ($o->isPaid()) {\n        return;\n    }\n    $this->chargeCard($o);\n    $this->sendReceipt($o);\n}\n// Or restructure so all paths are live:\nfunction processOrder(Order $o): void {\n    if (!$o->isPaid()) {\n        $this->chargeCard($o);\n        $this->sendReceipt($o);\n    }\n}",
    "example_note": "PHPStan level 4+ will report unreachable code. Delete it — git history preserves it if you ever need it back.",
    "quick_fix": "Delete it — dead code is not documentation, it is noise; git history preserves it if needed",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/dead_code",
        "html_url": "https://codeclaritylab.com/glossary/dead_code",
        "json_url": "https://codeclaritylab.com/glossary/dead_code.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Dead Code](https://codeclaritylab.com/glossary/dead_code) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/dead_code"
            }
        }
    }
}