Tangential comments about Software Development

Monday, December 31, 2012

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 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.

  1. Extract Class 
  2. Introduce Null Object 
  3. Move Field 
  4. move code into single class files 
  5. delete commented code
  6. Inline Method 
  7. Extract Method
  8. move code within file
  9. Extract Method
  10. Introduce Explaining Variable
  11. Replace Magic Number with Symbolic Constant
  12. Extract Method
  13. Move Field
  14. Inline Temp
  15. Extract Method
  16. replace class with struct
  17. 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:

Tests caught a mistake