Editorial note: I originally wrote this post for the NDepend blog. Go check it out over there and stay for a while to look around. If you like posts about code analysis, metrics, and the business of software development, you’ll like the collection of posts there.
One of the more puzzling misconceptions that I hear pertains to the topic of refactoring. I consult on a lot of legacy rescue efforts that will need to involve refactoring, and people in and around those efforts tend to think of “refactor” as “massive cleanup effort.” I suspect this is one of those conflations that happens subconsciously. If you actually asked some of these folks whether “refactor” and “massive cleanup effort” were synonyms, they would say no, but they never conceive of the terms in any other way during their day to day activities.
Let’s be clear. Here is the actual definition of refactoring, per wikipedia.
Code refactoring is the process of restructuring existing computer code – changing the factoring – without changing its external behavior.
Significantly, this definition mentions nothing about the scope of the effort. Refactoring is changing the code without changing the application’s behavior. This means the following would be examples of refactoring, provided they changed nothing about the way the system interacted with external forces.
- Renaming variables in a single method.
- Adding whitespace to a class for readability.
- Eliminating dead code.
- Deleting code that has been commented out.
- Breaking a large method apart into a few smaller ones.
I deliberately picked the examples above because they should be semi-understandable, even by non technical folks, and because they’re all scalable down to the tiny. Some of these activities could be done by a developer in under a minute. These are simple, low-effort refactorings.
Let’s now consider another definition of refactoring that can be found at Martin Fowler’s website.
Refactoring is a controlled technique for improving the design of an existing code base. Its essence is applying a series of small behavior-preserving transformations, each of which “too small to be worth doing”. However the cumulative effect of each of these transformations is quite significant.
I took the wikipedia definition and used it to suggest that refactorings could be small and low-effort. Fowler takes it a step further and suggests that they should be small and low effort. In fact, he suggests that they should be “too small to be worth doing.” That’s fascinating. Read More