A parameter is no longer used by the method body.
Programmers often add parameters but are reluctant to remove them. After all, a spurious parameter doesn’t cause any problems, and you might need it again later.
This is the demon Obfuscatis speaking; purge him from your soul! A parameter indicates information that is needed; different values make a difference. Your caller has to worry about what values to pass. By not removing the parameter you are making further work for everyone who uses the method. That’s not a good trade-off, especially because removing parameters is an easy refactoring.
The case to be wary of here is a polymorphic method. In this case you may well find that other implementations of the method do use the parameter. In this case you shouldn’t remove the parameter. You might choose to add a separate method that can be used in those cases, but you need to examine how your callers use the method to see whether it is worth doing that. If some callers already know they are dealing with a certain subclass and doing extra work to find the parameter or are using knowledge of the class hierarchy to know they can get away with a null, add an extra method without the parameter. If they do not need to know about which class has which method, the callers should be left in blissful ignorance.
- Check to see whether this method signature is implemented by a superclass or subclass. Check to see whether the class or superclass uses the parameter. If it does, don’t do this refactoring.
- Declare a new method without the parameter. Copy the old body of code to the new method.
If you need to remove more than one parameter, it is easier to remove them together.
- Change the body of the old method so that it calls the new one.
If you only have a few references, you can reasonably skip this step.
- Compile and test.
- Find all references to the old method and change them to refer to the new one. Compile and test after each change.
- Remove the old method.
If the old method is part of the interface and you cannot remove it, leave it in place and mark it as deprecated.
- Compile and test.
Because I’m pretty comfortable with adding and removing parameters, I often do a batch in one go.
|This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License|