When #React Meets #OOP : Shotgun Surgery
Problem:
Making any modifications requires that you make many small changes to many different #classes. Condition checks are spread in react components, hooks and helper functions. (Imagine you want to add a new yellow branch in fig 1, you need to update all three components).
Solution:
The Shotgun surgery strategy pattern consists of a few strategies that implement a common interface, and that interface has methods that can replace the if-else checks (or map looking-up) that were previously used in the code. Typically in OO languages, #Polymorphism is commonly used in OO languages to employ different instances of an interface at runtime.
How:
Use the Move Method ( i.e., Create a new method in the class that uses the method the most, and then move code from the old method to it). To combine existing class behaviours into a single class, convert the old method's code into a reference to the new method in the other class, or eliminate it entirely. If no class exists for this, build one.
If shifting code to the same class leaves the original classes mostly empty, try to eliminate the now-redundant classes.
A heartfelt thank you to all the veterans who have served and continue to serve.