Replace Parameter with Explicit Methods
A method is split into parts, each of which is run depending on the value of a parameter.
Extract the individual parts of the method into their own methods and call them instead of the original method.
A method containing parameter-dependent variants has grown massive. Non-trivial code is run in each branch and new variants are added very rarely.
- Improves code readability. It is much easier to understand the purpose of
When Not to Use
- Do not replace a parameter with explicit methods if a method is rarely changed and new variants are not added inside it.
How to Refactor
For each variant of the method, create a separate method. Run these methods based on the value of a parameter in the main method.
Find all places where the original method is called. In these places, place a call for one of the new parameter-dependent variants.
When no calls to the original method remain, delete it.