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. The more distinct responsibilities a method mixes, the more state and branching a reader has to track before changing any one line.
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. Untested branches inside the method are where regressions slip through.
Larger units carry more defects in absolute terms, so a long method tends to be where a module's bugs are found and fixed. The bugs hide in the interactions between the method's many responsibilities.
Specific Cost Mechanisms
- ●Developer comprehension time: extra minutes per developer per week for each long method in active development
- ●Bug clustering: defects concentrate in the long methods that change most, and each one has to be traced through the whole method to fix
- ●Code review delays: reviewers must understand the full method to verify any change inside it
Estimated Annual Cost
Cost of one instance a year by team size, from our model at $180K fully-loaded cost per engineer. Low and high use the bottom and top of the High inputs; mid is halfway. See full methodology.
| Team Size | Low | Mid | High |
|---|---|---|---|
| 3 devs | $3K | $7K | $11K |
| 5 devs | $4K | $10K | $16K |
| 8 devs | $6K | $15K | $24K |
| 10 devs | $7K | $19K | $30K |
| 20 devs | $14K | $35K | $57K |
These are model outputs, not measurements. Every input is on the cost model page, and the same model produces the figures on the 22 smells page. Put in your own team and cost in the smell profile calculator.
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