Code Smell Shotgun Surgery
Shotgun Surgery resembles Divergent Change but is actually the opposite smell. Divergent Change is when many changes are made to a single class. Shotgun Surgery refers to when a single change is made to multiple classes simultaneously.
Signs and Symptoms
Making any modifications requires that you make many small changes to many different classes.
Reasons for the Problem
A single responsibility has been split up among a large number of classes. This can happen after overzealous application of Divergent Change.
- Use Move Method and Move Field to move existing class behaviors into a single class. If there is no class appropriate for this, create a new one.
- If moving code to the same class leaves the original classes almost empty, try to get rid of these now-redundant classes via Inline Class.
- Better organization.
- Less code duplication.
- Easier maintenance.
Tired of reading?
No wonder, there are 7 hours worth of the text on this website.
Try out something different. We've just launched the interactive learning course on refactoring. It has more content and much more fun than a boring text.Learn more...