{
    "slug": "duplicate_code",
    "term": "Duplicate Code",
    "category": "quality",
    "difficulty": "beginner",
    "short": "Identical or near-identical blocks of code in multiple places — the most common source of maintenance bugs.",
    "long": "When the same logic appears in multiple places, a bug fix or requirement change must be applied everywhere it exists — and inevitably one copy is missed. Duplicate code also makes understanding a codebase harder because readers must verify that two similar-looking blocks are truly identical in behaviour. The fix is to extract the duplicated logic into a named function or method, then call it from all the original sites.",
    "aliases": [
        "copy-paste code",
        "code duplication",
        "cloned code"
    ],
    "tags": [
        "code-smell",
        "dry",
        "refactoring"
    ],
    "misconception": "Extracting all duplicate code into a shared function always improves design. Two identical snippets in unrelated modules may represent different concepts that happen to look alike today but will diverge — forcing them together creates hidden coupling.",
    "why_it_matters": "Duplicate code means a bug fix or rule change must be made in multiple places — one copy always gets missed, causing inconsistent behaviour and maintenance debt that compounds with every new duplication.",
    "common_mistakes": [
        "Copy-pasting a code block with minor variations instead of extracting a parameterised function.",
        "Duplicate validation logic in both the model and the controller — they diverge over time.",
        "Near-duplicate switch statements for related concepts — use a strategy pattern or data-driven lookup.",
        "Not using array_map/array_filter/array_reduce and writing the same loop logic in multiple places."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "code_smell",
        "dead_code"
    ],
    "prerequisites": [
        "dry",
        "extract_function",
        "refactoring"
    ],
    "refs": [
        "https://refactoring.guru/smells/duplicate-code"
    ],
    "bad_code": "// In UserController\n$user = User::find($id);\nif (!$user) { abort(404); }\n\n// In PostController (exact copy)\n$user = User::find($id);\nif (!$user) { abort(404); }",
    "good_code": "// Extracted helper or base controller method\npublic function findOrFail(int $id): User {\n    return User::find($id) ?? abort(404);\n}\n// Or use Eloquent's built-in:\n$user = User::findOrFail($id);",
    "quick_fix": "Extract the duplicated block into a shared method, trait, or parent class; if context differs slightly use a parameter to handle the variation",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/duplicate_code",
        "html_url": "https://codeclaritylab.com/glossary/duplicate_code",
        "json_url": "https://codeclaritylab.com/glossary/duplicate_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": "[Duplicate Code](https://codeclaritylab.com/glossary/duplicate_code) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/duplicate_code"
            }
        }
    }
}