CouplersHigh

Feature Envy: What It Costs and How to Fix It

A method that uses data and methods from another class more than from its own class.

Annual Cost$3k - $24k
Severity
4/5
CategoryCouplers
Detection4 tools

What It Is

Feature Envy occurs when a method in Class A spends most of its time working with data from Class B. The method reaches into Class B, extracts values, performs calculations, and possibly puts results back. The method 'envies' Class B's features and should probably live in Class B instead. Feature Envy is one of the most common coupling smells and is a strong indicator that responsibilities are misallocated between classes.

Threshold: When a method accesses 3+ fields or methods of another class, and fewer than 2 of its own, it has Feature Envy. The 'principle of proximity' applies: logic should live near the data it operates on.

Why It Costs Money

1

Changes to Class B's structure break Class A. When Feature Envy exists, Class A depends on the internal structure of Class B. Any change to B's fields or methods requires updating A, even though A should not care about B's internals.

2

Logic duplication follows. When multiple classes envy the same features of Class B, the same logic appears in multiple places. Each envious method implements its own version of the calculation, leading to inconsistency.

3

Testing becomes coupled. To test the envious method in Class A, you must set up Class B with specific state. This creates brittle tests that break when Class B changes, even though the test is nominally for Class A.

Specific Cost Mechanisms

  • Structural coupling: each change to Class B potentially breaks envious methods in other classes
  • Logic scattering: the same calculation in multiple envious methods creates inconsistency bugs
  • Test brittleness: tests for envious methods break when the envied class changes

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$3,000$7,200$14,400
5 devs$5,000$12,000$24,000
10 devs$7,500$18,000$24,000
20 devs$10,000$24,000$24,000

How to Detect It

Specific rules and thresholds for automated detection. See full tool comparison.

SonarQube
squid:S1200

High class coupling indicates potential Feature Envy

CodeClimate
feature-envy

Directly detects methods that access foreign data more than local data

PMD
LawOfDemeter

Method chain length indicates reaching into other objects

IntelliJ IDEA
Feature envy inspection

Detects methods with more foreign references than local

Refactoring Patterns

Proven techniques to eliminate this smell. See all refactoring patterns.

Move Method

The method clearly belongs in the class it envies

Effort: 1-3 hours
Impact: Eliminates the coupling entirely

Extract Method then Move

Only part of the method envies another class

Effort: 2-4 hours
Impact: Separates the envious logic and relocates it

Introduce Delegate

Moving the method would create a circular dependency

Effort: 2-4 hours
Impact: Maintains separation while reducing direct access to foreign data

Related Smells