The first step in refactoring is to build a solid set of unit tests around the code you want to refactor.
"Whenever I do Refactoring, the first step is always the same. I need to build a solid set of tests for that section of code. The tests are essential because even though I follow Refactorings structured to avoid most of the opportunities for introducing bugs, I'm still human and still make mistakes. This I need solid tests."
- Martin Fowler, Refactoring : Improving the Design of existing Code (p7)
Wow, this is an incredible little paragraph tucked away in the first few pages of the book.
- First, we learn why unit testing is so important- because it is essential to refactoring!
- Then he mentions how having unit tests in place can help you to avoid the undetected creation of many bugs in the first place.
- Finally, we see Martin Fowler's humble writing style where he mentions that he uses unit tests because he makes mistakes, as if he is admitting that you, the reader, are more clever and and careful of a programmer.