I recently screwed up when applying an automatic refactoring. I would have thought that my unit tests would have detected the error but the refactoring also changed my test suite to accept the erroneous behaviour.

Automatic refactoring tools are great, but they're not perfect, and if you rely on tests to catch bad refactorings, refactoring your tests at the same time can easily mask errors.

What's the solution?

Firstly, I've got to stop mindlessly hitting the Ok button when refactoring! A skim through the list of refactorings to be applied would have shown me the error before it happened. Some refactorings are safer than others, but any refactoring that affects string constants needs careful review because it cannot use the type system to ensure safety. Perhaps I can configure my IDE to be more "in your face" when a refactoring affects string constants.

Secondly, I'm going to try refactoring my code separately from my tests. If the tests fail as I expect, I will then apply the refactoring to my tests as well. It might slow me down a bit, but the extra step will force me to check the effect of refactoring.

Refactoring is a case of more haste, less speed.

Copyright © 2004 Nat Pryce. Posted 2004-06-11. Share it.