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

Method Length Guidelines

style Beginner

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 17
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 1 ping M 0 pings T 0 pings W 3 pings T 0 pings F 1 ping S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 6 Perplexity 3 ChatGPT 3 Ahrefs 2 Google 2
crawler 13 crawler_json 3
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