← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Method Length Guidelines

Style Beginner
debt(d5/e3/b3/t5)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5), because detection_hints lists phpmd, phpcs, phploc, and sonarqube — all specialist static analysis tools that must be explicitly configured and run; the violation is not caught by the compiler or a default linter, but these tools make it reliably detectable when used.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3), because the quick_fix describes identifying a cohesive block and extracting it into a private method — a localised refactor within one component/file rather than a one-line patch or cross-cutting change. Common mistakes like inlining instead of extracting confirm the fix requires deliberate, targeted restructuring.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3), because method length is a per-method concern within individual classes. While it applies across all contexts (web, cli, queue-worker), each instance of a long method is an isolated structural debt that burdens maintainers of that specific class rather than imposing a gravitational pull on the entire codebase.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5), because the misconception field explicitly identifies the canonical wrong belief — that method length limits are arbitrary rules with no real benefit. Competent developers who haven't internalised clean-code principles often resist extraction, inline complexity rather than extracting it, or use comments as section dividers instead of extracting methods — documented gotchas that most developers eventually learn.

About DEBT scoring →

Also Known As

function length method size short methods

TL;DR

Short methods (ideally under 10–20 lines) are easier to understand, test, and name — length is a proxy for complexity.

Explanation

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.

Common 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.

Code Examples

✗ Vulnerable
// 80-line method doing too many things
public function processOrder($cart, $user, $payment) {
    // 15 lines: validate cart
    // 20 lines: apply discounts
    // 15 lines: process payment
    // 20 lines: send notifications
    // 10 lines: log audit trail
}
✓ Fixed
// Extract each concern into its own method
public function processOrder(Cart $cart, User $user, PaymentMethod $payment): Order {
    $validated = $this->validateCart($cart);
    $discounted = $this->applyDiscounts($validated, $user);
    $charged    = $this->processPayment($discounted, $payment);
    $this->notifyUser($charged, $user);
    $this->logAudit($charged, $user);
    return $charged;
}

// Each extracted method: 5-15 lines, one clear purpose
// Guideline: if a method needs a comment to explain sections, extract those sections

Added 15 Mar 2026
Edited 22 Mar 2026
Views 37
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 1 ping S 1 ping M 2 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 1 ping M 1 ping T 0 pings W
No pings yet today
PetalBot 1
Amazonbot 7 Ahrefs 4 Perplexity 3 ChatGPT 3 Scrapy 3 Google 2 Claude 2 SEMrush 2 Bing 1 Meta AI 1 Majestic 1 Sogou 1 PetalBot 1
crawler 26 crawler_json 5
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ 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
📦 Applies To
any web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Methods >20 lines in PHPmd output; methods requiring vertical scrolling; method with multiple comment sections dividing it into parts
Auto-detectable: ✓ Yes phpmd phpcs phploc sonarqube
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✗ Manual fix Fix: Medium Context: Function Tests: Update


✓ schema.org compliant