{
    "slug": "code_smell",
    "term": "Code Smell",
    "category": "quality",
    "difficulty": "beginner",
    "short": "A surface indication in code that usually corresponds to a deeper design problem — not a bug, but a maintainability risk.",
    "long": "The term was popularised by Martin Fowler in \"Refactoring\". A code smell is not necessarily broken — the code may work — but it suggests structural issues that will make the code harder to change safely over time. Common smells: long methods, large classes, long parameter lists, feature envy, data clumps, primitive obsession, and duplicate code. Smells are signals to investigate, not mandates to refactor immediately.",
    "aliases": [
        "code smells",
        "anti-pattern indicator",
        "refactoring trigger"
    ],
    "tags": [
        "refactoring",
        "quality",
        "principles"
    ],
    "misconception": "A code smell means the code is definitely wrong and must be fixed immediately. Smells are indicators that warrant investigation — context matters. A long method in a performance-critical parser may be intentional and correct.",
    "why_it_matters": "Code smells are early warning signs of design problems that compound over time — ignoring them leads to systems where every change risks breaking something else. Addressing smells proactively is ten times cheaper than refactoring under pressure.",
    "common_mistakes": [
        "Treating all code smells as equally urgent — prioritise smells in frequently-changed, business-critical code.",
        "Refactoring smells without tests in place — you need a safety net before restructuring.",
        "Conflating code style violations (formatting) with structural smells — they require different remediation.",
        "Using smell detection tools as a pass/fail gate rather than a signal to investigate."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "god_class",
        "feature_envy",
        "duplicate_code",
        "dead_code",
        "magic_number"
    ],
    "prerequisites": [
        "refactoring",
        "technical_debt",
        "boy_scout_rule"
    ],
    "refs": [
        "https://refactoring.guru/refactoring/smells",
        "https://martinfowler.com/books/refactoring.html"
    ],
    "bad_code": "// Multiple smells in one function:\nfunction p(\\$d) {  // cryptic name\n    global \\$db;   // global state\n    if(\\$d['t']==1) { // magic number\n        \\$r = \\$db->query(\"SELECT * FROM orders WHERE uid=\".\\$d['u']); // SQL injection\n        foreach(\\$r as \\$row) { echo \\$row['total'] * 0.9; } // magic number\n    }\n}",
    "good_code": "function applyLoyaltyDiscount(array \\$request): void {\n    \\$orders   = \\$this->orderRepo->findByUserId(\\$request['user_id']);\n    \\$discount = LoyaltyDiscount::RATE; // named constant\n    foreach (\\$orders as \\$order) {\n        echo \\$order->total * (1 - \\$discount);\n    }\n}",
    "quick_fix": "Use the catalogue: long method, god class, duplicate code, feature envy, data clumps, primitive obsession, switch statements, parallel inheritance, lazy class, speculative generality",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-13",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/code_smell",
        "html_url": "https://codeclaritylab.com/glossary/code_smell",
        "json_url": "https://codeclaritylab.com/glossary/code_smell.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": "[Code Smell](https://codeclaritylab.com/glossary/code_smell) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/code_smell"
            }
        }
    }
}