Refactoring by the Book
I knew it was time to refactor some code. As an exercise, I decided to record each change, and then compare them with the list at Martin Fowler's Refactoring.com. Here's my list, where the Title Case names are ones in the book, the lower case names are not mentioned, probably because they're too trivial to mention.
- Extract Class
- Introduce Null Object
- Move Field
- move code into single class files
- delete commented code
- Inline Method
- Extract Method
- move code within file
- Extract Method
- Introduce Explaining Variable
- Replace Magic Number with Symbolic Constant
- Extract Method
- Move Field
- Inline Temp
- Extract Method
- replace class with struct
- Replace Constructor with Factory Method
This took 90 minutes. In the process, a 54-line method became 18 lines. The Move Field operation was one I verified with my tests, running them for hard coded true and false values, and seeing that in each case there was a fail, whereas when I used the moved value all tests passed. The last Extract Method was the place the tests caught a mistake I made: