Refactoring Rename Method
ProblemThe name of a method does not explain what the method does.
SolutionRename the method.
Perhaps a method was poorly named from the very beginning – for example, someone created the method in a rush and did not give proper care to naming it well.
Or perhaps the method was well named at first but as its functionality grew, the method name stopped being a good descriptor.
- Code readability. Try to give the new method a name that reflects what it does. Something like
How to Refactor
See whether the method is defined in a superclass or subclass. If so, you must repeat all steps in these classes too.
The next method is important for maintaining the functionality of the program during the refactoring process. Create a new method with a new name. Copy the code of the old method to it. Delete all the code in the old method and, instead of it, insert a call for the new method.
Find all references to the old method and replace them with references to the new one.
Delete the old method. If the old method is part of a public interface, do not perform this step. Instead, mark the old method as deprecated.
Tired of reading?
No wonder, there are 7 hours worth of the text on this website.
Try out something different. We've just launched the interactive learning course on refactoring. It has more content and much more fun than a boring text.Learn more...
Everything is quite simple. This example is like video (but much cooler):
- After pressing the Play button, you will see floating messages, which will guide you through the example. You can proceed by clicking on them.
- You can fast-forward or return to previous steps by pressing arrow buttons on the left.
- Also, you can take a look at the code diff window, which will show what has changed in code during the example ()
- To check the code for errors, you can press "Compile and test" button ()