Section 6.5 The Introduce Parameter and Inline refactorings
There are two more refactorings to discuss in this section, introducing a parameter and inlining.
Subsection 6.5.1 Introduce Parameter refactoring
Introduce Parameter is similar to what we have seen so far.
Introduce Parameter: Identify a local variable which on hindsight should have been provided by the caller instead. Then turn it into a parameter.
The mechanics are relatively simple:
- Find the place where the local variable was initialized, and copy that initial value.
- Introduce a new parameter of the same type and name as the local variable.
- Find all calls to your method and paste the initial value.
- If that initial value depended on things that are not available at the callers, you’ll need to rethink what you are trying to do.
- Remove the declaration and initialization of the local variable.
- If your method was overriding a superclass’ method, you’ll need to adjust that method as well.
Subsection 6.5.2 Inlining refactoring
Lastly, inlining has a couple of variations:
- Inline variable or parameter: Identify a local variable or parameter that always has the same value or calculation. Then replace it with that value or calculation.
- Inline method. Replace a call to a method with the code contained in that method’s body, adjusting the parameter usages to use the provided argument values.
Inlining is often done in preparation of other refactorings, or as a cleanup phase in the aftermath of other refactorings.