Shotgun Surgery
Shotgun Surgery is usually a more obvious symptom that reveals another smell.
Symptoms
- You have to make the same small change across several different files.
- Changes become difficult to manage because they are hard to keep track of.
Make sure you look for related smells in the affected code:
Solutions
- Replace Conditional with Polymorphism to replace duplicated case statements and if-elsif blocks.
- Replace Conditional with Null Object if changing a method to return nil would require checks for nil in several places.
- Extract Decorator to replace duplicated display code in views/templates.
- Introduce Parameter Object⭐ to hang useful formatting methods along side a data clump of related attributes.
- Use Convention Over Configuration to eliminate small steps that can be inferred based on a convention such as a name.
- Inline Class if the class only serves to add extra steps when performing changes.
Prevention
- If your changes become spread out because you need to pass information between boundaries for dependencies, try inverting control.
- If you find yourself repeating the exact same change in several places, make sure that youIf you find yourself repeating the exact same change in several places, make sure that you Don’t Repeat Yourself
- If you need to change several places because of a modification in your dependency chain, such as changing
user.plan.price
touser.account.plan.price
, make sure that you’re following the Law of Demeter - If conditional logic is affected in several places by a single, cohesive change, make sure that you’re following Tell, Don’t Ask