Divergent Change Smell
What you'll learn: You'll discover how to spot classes that break apart under pressure from multiple directions, signaling a violation of the Single Responsibility Principle.
What Is Divergent Change?
Imagine a Swiss Army knife that you constantly need to modify—sometimes you're reshaping the blade, other times you're adjusting the corkscrew, and occasionally you're fixing the scissors. Each change is for a completely different reason, yet they all require opening up the same tool.
Divergent Change occurs when a single class frequently needs modification for different, unrelated reasons. If you find yourself saying, "Whenever we change X, we have to modify ClassA. And whenever we change Y, we also have to modify ClassA. Oh, and when Z changes..." you've found this smell.
The Core Problem
This smell directly violates the Single Responsibility Principle (SRP). A class should have only one reason to change. When multiple concerns converge in one place, the class becomes:
- Fragile: Changes in one area risk breaking unrelated functionality
- Hard to understand: Mixed responsibilities create cognitive overload
- Difficult to reuse: You can't extract just the piece you need
Example Without Code
Consider a UserAccount class that changes when:
- Database schema updates (persistence logic)
- Business rules for user validation evolve
- Email notification templates are redesigned
- Authentication protocols are upgraded
Each concern pulls the class in a different direction—hence "divergent."
How to Fix It
Extract Class is your primary refactoring tool. Split the overloaded class into focused classes, each handling one responsibility. Your UserAccount might become: UserData, UserValidator, UserNotifier, and UserAuthenticator.
Key Takeaway: When a class changes for multiple unrelated reasons, it suffers from Divergent Change—split it into focused classes, each with a single responsibility.