When to Refactor (And When Not To)

Every developer has felt the urge to rewrite code that offends their sensibilities. That urge is natural, but acting on it without good reason can cause more problems than it solves. Here’s how we think about when refactoring makes sense—and when it doesn’t.

Good Reasons to Refactor

You’re about to make a change in that area anyway. The Boy Scout Rule (“leave the code better than you found it”) works well when applied locally. If you’re touching code to add a feature or fix a bug, improve it while you’re there.