{
    "slug": "long_method",
    "term": "Long Method",
    "category": "quality",
    "difficulty": "beginner",
    "short": "A function or method that is too long to understand in one reading — typically 20+ lines is a signal to split.",
    "long": "Long methods are one of the most common code smells. A method doing many things is hard to name accurately, hard to test in isolation, and hard to reuse. As a rough guide: if you cannot see the entire method without scrolling, it's worth examining. The refactoring is Extract Method — identify a coherent block of lines that does one thing, give it a descriptive name, and move it to a private helper. Good method names make the original method read like a table of contents.",
    "aliases": [
        "long function",
        "oversized method",
        "god method"
    ],
    "tags": [
        "code-smell",
        "refactoring",
        "solid"
    ],
    "misconception": "A long method is fine if it is well-commented. Comments compensate for poor structure but do not fix it — a 200-line method with comments is still untestable in parts, hard to name meaningfully, and difficult to change safely.",
    "why_it_matters": "Long methods are harder to understand, test, and reuse — each additional line increases the number of states a reader must track simultaneously, exponentially raising the chance of missing a bug.",
    "common_mistakes": [
        "Adding logic inline instead of extracting a named method — 'it's only a few lines' compounds over time.",
        "Methods that handle multiple abstraction levels: high-level orchestration mixed with low-level detail.",
        "Not using early return to reduce nesting — deep nesting is a long-method symptom.",
        "Test methods that are as long as the code they test — long tests indicate long production code."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "code_smell",
        "cyclomatic_complexity",
        "cognitive_complexity"
    ],
    "prerequisites": [
        "extract_function",
        "single_responsibility",
        "cyclomatic_complexity"
    ],
    "refs": [
        "https://refactoring.guru/smells/long-method"
    ],
    "bad_code": "public function checkout(Cart $cart): Receipt {\n    // validate items (20 lines)\n    // calculate discounts (15 lines)\n    // charge card (10 lines)\n    // send email (15 lines)\n    // update inventory (10 lines)\n    // log audit (5 lines)\n    // 75 lines total\n}",
    "good_code": "public function checkout(Cart $cart): Receipt {\n    $this->validateItems($cart);\n    $total   = $this->calculateTotal($cart);\n    $charge  = $this->chargeCard($cart->customer, $total);\n    $receipt = Receipt::create($charge);\n    $this->mailer->sendReceipt($receipt);\n    $this->inventory->deduct($cart);\n    $this->audit->log('checkout', $receipt);\n    return $receipt;\n}",
    "example_note": "Each extracted method is individually testable and the checkout method reads like a table of contents.",
    "quick_fix": "Extract blocks of code with a comment above them into their own named private methods — the comment becomes the method name",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/long_method",
        "html_url": "https://codeclaritylab.com/glossary/long_method",
        "json_url": "https://codeclaritylab.com/glossary/long_method.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": "[Long Method](https://codeclaritylab.com/glossary/long_method) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/long_method"
            }
        }
    }
}