Test-driven development teaches the red-green-refactor
mantra, which works wonderfully when starting a project from scratch.
Chances are good, however, that you’ll spend 80% of your programming
career working in existing codebases. In existing code, I’ve found it
easier to refactor first until the desired behavior change is a
30-minute task. By refactoring first, you’ll…
- better understand the code you’re about to change.
- better understand the consequences of your change.
- make it easier for the person reviewing your change to understand
its impact. (It’s much easier to review several independent
refactorings and one simple change than a nonobvious change followed
by refactorings; or, worse, all of the above in one commit.)
- reduce risk and save time by preventing unexpected bugs.
Red-green-refactor is great, but try refactoring first!