Long Method: What It Costs and How to Fix It
Methods that have grown too long to understand at a glance, typically exceeding 20-30 lines.
What It Is
A Long Method is any function or method that has accumulated so many lines of code that reading it requires scrolling, mental bookmarking, and significant cognitive effort. The threshold varies by language and team convention, but most static analysis tools flag methods above 20-30 lines. The problem is not length itself but the number of distinct responsibilities hidden inside a single method. When a method does five things, every developer who touches it must understand all five, even if they only need to change one.
Why It Costs Money
Every developer who reads the method must hold the entire control flow in working memory. Studies on cognitive load show that comprehension time scales superlinearly with method length: a 100-line method takes 4-5x longer to understand than a 20-line method, not 5x.
Long methods resist unit testing. When a method has multiple code paths interleaved together, achieving full branch coverage requires complex test setups with many mocks. Teams with long methods consistently report 30-40% lower test coverage in affected modules.
Bug density correlates with method length. Research on open-source Java projects found that methods above 50 lines contained 2.5x more defects per line than methods under 15 lines. The bugs hide in the interactions between the method's many responsibilities.
Specific Cost Mechanisms
- ●Developer comprehension time: 15-25 extra minutes per developer per week per long method in active development
- ●Bug clustering: 2.5x higher defect density means more production incidents, each costing 4-8 hours of investigation and fix time
- ●Code review delays: PRs touching long methods take 40-60% longer to review because reviewers must understand the full method to verify any change
Estimated Annual Cost
Cost per instance by team size and codebase size. Based on $120,000 average developer salary. See full methodology.
| Team Size | Small (<50k LOC) | Medium (50k-200k) | Large (200k+) |
|---|---|---|---|
| 3 devs | $4,800 | $9,600 | $19,200 |
| 5 devs | $8,000 | $16,000 | $32,000 |
| 10 devs | $12,000 | $24,000 | $38,000 |
| 20 devs | $16,000 | $32,000 | $38,000 |
How to Detect It
Specific rules and thresholds for automated detection. See full tool comparison.
Function length threshold (default 200 lines, recommend lowering to 30)
Default threshold 25 lines, configurable per language
Set max to 30-40 for JS/TS codebases
Default threshold 100 lines
Refactoring Patterns
Proven techniques to eliminate this smell. See all refactoring patterns.
Extract Method
Identifiable blocks of code that serve a single purpose within the long method
Replace Temp with Query
Temporary variables used to store intermediate calculations that could be separate methods
Decompose Conditional
Complex if/else chains with substantial bodies