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.
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.
Why It Costs Money
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.
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.
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 Size | Small (<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.
High class coupling indicates potential Feature Envy
Directly detects methods that access foreign data more than local data
Method chain length indicates reaching into other objects
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
Extract Method then Move
Only part of the method envies another class
Introduce Delegate
Moving the method would create a circular dependency