Change Preventers: Divergent Change and Shotgun Surgery
What you'll learn: How to spot two opposite but equally problematic code smells that make your codebase resist change.
The Problem
Imagine you need to make a simple change—like adding a new payment method to your e-commerce system. Ideally, you'd touch one or two well-defined places. But sometimes the code fights back in two distinct ways.
Divergent Change
Divergent Change occurs when one class needs to be modified for many different reasons. Every time a requirement changes—whether it's database logic, business rules, or UI formatting—you find yourself opening the same class.
Think of a Swiss Army knife that keeps getting new tools welded onto it. It started simple, but now it's a bloated mess that needs modification whenever any feature requires work.
Example scenario: Your OrderProcessor class changes when you add payment methods, when shipping rules update, when tax calculations change, and when notification preferences evolve. One class, many reasons to change—violating the Single Responsibility Principle.
Shotgun Surgery
Shotgun Surgery is the opposite: one conceptual change requires modifying many unrelated classes. To implement a single feature, you must hunt through dozens of files, making tiny edits everywhere.
Imagine adjusting your thermostat requires you to tweak settings in every room individually. Exhausting and error-prone.
Example scenario: Adding a new user role requires changes in authentication logic, the UI layer, database queries, logging code, and configuration files scattered across your codebase.
Recognition
- Divergent Change: One class has a long commit history with unrelated changes.
- Shotgun Surgery: A feature branch touches 15+ files for one logical change.
Key Takeaway: Divergent Change means one class does too much; Shotgun Surgery means related code is scattered everywhere. Both make your code fragile and exhausting to maintain.