2024 Public Training Schedule
December 9 – 12, 2024 – Agile Analysis and Design Patterns
Half-Day Sessions
(c) 2024 To Be Agile
One of the best opportunities to refactor code is when you’re starting to get to know it. This not only helps improve the code but it also improves your understanding of the code and helps everyone else who touches the code in the future.
There are many kinds of refactorings that one can do to existing code that is not under test which are safe and helpful for embedding understanding code. When we have a bunch of existing code that’s not under test it can be dangerous to make major changes to it but these safer refactorings allow us to make small, safe, and incremental changes to code in ways that can also help us learn a system.
One of my very favorite refactorings to help me learn a system is Rename. Rename is a really simple refactoring but one I use all the time. It’s not uncommon to find methods that have misleading names and when I come across this I often use the automated Rename Method refactoring in my IDE to change the name to one makes more sense. I find that just this one refactoring, Renaming Methods, can vastly improve the clarity of a system and that helps me learn it much faster.
Since the Rename refactoring is a nondestructive refactoring and one that many IDE’s have automated, it is very safe to use, even on software that’s not under test. However, if I’m very concerned about the safety of a method and I may have callers that are external to my code then one thing I can do is leave the existing method alone and simply wrap it in another method of my own with the name of my choosing. That way I can change all of my internal callers to use the wrapping method with the new name so that it’s clearer on what they’re actually doing and external callers are still able to call the original method with its original name directly.
I and others have written a lot about what makes a good name for a method. I won’t go into that now except to say in a nutshell that the goal of the name of a method should clearly state what it does. Think of a method name as a promise to provide some kind of behavior or service to its callers. When a behavior is given the right name then much of the system starts to make sense that didn’t before.
Renaming methods can help bring clarity to legacy code and is a really excellent way to improve my knowledge of that code while making it just a little bit better for the next person who comes along and needs to work with it. Because Rename is among the safest of all the refactorings, I feel that I can use it in almost any situation and this helps me embed knowledge into an existing system, improving the system, as well as improving my understanding of what the system does.
Note: This blog post is based on one of the “Seven Strategies…” sections in my book, Beyond Legacy Code: Nine Practices to Extend the Life (and Value) of Your Software.
Previous Post: « Why Practice 9: Refactor Legacy Code
Next Post: Refactor to Make Small Improvements »