BloatersHigh

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.

Annual Cost$4.8k - $38k
Severity
4/5
CategoryBloaters
Detection4 tools

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.

Threshold: Most teams flag methods above 30 lines as warranting review. Methods above 50 lines are nearly always worth splitting. The key indicator is not raw line count but the number of distinct responsibilities.

Why It Costs Money

1

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.

2

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.

3

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 SizeSmall (<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.

SonarQube
squid:S138 / squid:S1151

Function length threshold (default 200 lines, recommend lowering to 30)

CodeClimate
method-lines

Default threshold 25 lines, configurable per language

ESLint
max-lines-per-function

Set max to 30-40 for JS/TS codebases

PMD
ExcessiveMethodLength

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

Effort: 15-45 minutes per extraction
Impact: 20-40% reduction in comprehension time per extraction

Replace Temp with Query

Temporary variables used to store intermediate calculations that could be separate methods

Effort: 10-20 minutes per variable
Impact: Eliminates hidden dependencies between code blocks

Decompose Conditional

Complex if/else chains with substantial bodies

Effort: 20-60 minutes per conditional
Impact: Reduces cyclomatic complexity by 30-50% per decomposition

Related Smells