jeudi 29 octobre 2015

Refactoring, you're doing it wrong (en)

Some developers consider that refactoring is a huge chunk of work that needs to freeze the repository during a week. A week when every features are broken and rebuilt. Afterwards, another week is needed to merge the new features, once pushing on the central repo is authorized again. That’s so wrong, so costly!

It reminds me a talk by Joshua Kerievsky in which he explains more or less that I would try to remain the shortest time possible in the red while refactoring. With this principle, he keeps the ability to deploy continuously while refactoring the whole system. It’s been revelation for me. I like to feel I can deploy whenever I want, even if I don’t have the need to do so. In fact, I do not want to be in a situation where I block the repository, where I prevent my team mates to commit, where I’m sure I will spend painful hours to merge others changes one by one without feeling safe about it.

The ideas:

  • Refactor in small steps. Consider that you will continuously refactor the application and not perform a big bang refactoring once in a while. Think boy scout rule.
  • Consider refactoring only if you have a decent code coverage on the part you want. If you don’t, start with writing a test harness. At least system tests that will experience the code from the UI and check the outputs or the database. If you want to do refactoring that matters, you have to be damn sure that your are not slaughtering the application.
  • Use your IDE. You have to know every features that lies in the Refactoring menu and use them rather than manual editing. Tooling won’t take any risk and they will ask you if they’re not sure of what they’re doing.
  • Consider the “I build a huge amount of code that will work in parallel of the current system that I’ll branch when it’s ready” only when you have no simpler solution left.

Timebox your refactoring session. I promise you that you can do significant improvement within 30 minutes. Tell yourself that delivery is tomorrow, or in 1 hour or in 30 minutes. It will keep you motivated

(this post is a translation of an older one)