{
    "slug": "method_length",
    "term": "Method Length Guidelines",
    "category": "style",
    "difficulty": "beginner",
    "short": "Short methods (ideally under 10–20 lines) are easier to understand, test, and name — length is a proxy for complexity.",
    "long": "Long methods are one of the oldest and most reliable code smells. Martin Fowler recommends that any method longer than five lines is worth scrutinising; practically, methods over 20 lines should be refactored. Long methods almost always have multiple responsibilities that can be extracted into well-named helper methods. Each extracted method should be small enough to have a name that fully describes its purpose — if you cannot name it clearly, it may still be doing too much. Cyclomatic complexity is often more meaningful than raw line count — a 30-line linear method may be fine; a 10-line method with 6 branches is complex.",
    "aliases": [
        "function length",
        "method size",
        "short methods"
    ],
    "tags": [
        "style",
        "refactoring",
        "readability",
        "quality"
    ],
    "misconception": "Method length limits are arbitrary rules with no real benefit. Short methods with good names are individually testable, composable, and understandable in isolation — a 200-line method forces a reader to hold the entire function in working memory at once.",
    "why_it_matters": "Short methods have one clear purpose, a single level of abstraction, and can be read without scrolling — long methods mix abstraction levels and accumulate multiple reasons to change.",
    "common_mistakes": [
        "Not extracting private methods when a function exceeds 20-30 lines.",
        "Comments used to separate 'sections' of a long method — each section is a candidate for extraction.",
        "Test methods as long as the code they test — long test methods indicate the production code does too much.",
        "Reducing method length by inlining logic rather than extracting it — moves complexity, doesn't reduce it."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "long_method",
        "cyclomatic_complexity",
        "single_responsibility",
        "extract_function"
    ],
    "prerequisites": [
        "long_method",
        "extract_function",
        "cognitive_complexity"
    ],
    "refs": [
        "https://refactoring.guru/smells/long-method"
    ],
    "bad_code": "// 80-line method doing too many things\npublic function processOrder(\\$cart, \\$user, \\$payment) {\n    // 15 lines: validate cart\n    // 20 lines: apply discounts\n    // 15 lines: process payment\n    // 20 lines: send notifications\n    // 10 lines: log audit trail\n}",
    "good_code": "// Extract each concern into its own method\npublic function processOrder(Cart \\$cart, User \\$user, PaymentMethod \\$payment): Order {\n    \\$validated = \\$this->validateCart(\\$cart);\n    \\$discounted = \\$this->applyDiscounts(\\$validated, \\$user);\n    \\$charged    = \\$this->processPayment(\\$discounted, \\$payment);\n    \\$this->notifyUser(\\$charged, \\$user);\n    \\$this->logAudit(\\$charged, \\$user);\n    return \\$charged;\n}\n\n// Each extracted method: 5-15 lines, one clear purpose\n// Guideline: if a method needs a comment to explain sections, extract those sections",
    "quick_fix": "Target methods under 20 lines — if it's longer, find a cohesive block with a describable purpose and extract it into a private method with a name that explains what it does",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/method_length",
        "html_url": "https://codeclaritylab.com/glossary/method_length",
        "json_url": "https://codeclaritylab.com/glossary/method_length.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": "[Method Length Guidelines](https://codeclaritylab.com/glossary/method_length) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/method_length"
            }
        }
    }
}