DEV Community

Discussion on: When to refactor

Collapse
 
kallmanation profile image
Nathan Kallman

I think there's two times to refactor:

  1. Right after writing code. It's in the phrase red-green-refactor. Said another way that's: figure out what to do, then do it as naively as possible, then figure out how to make it more readable/more modular/more extendable/more reusable/more performant.

  2. Right before writing code. I like to say, "Make it easy; then make the change". Often more junior devs will take a requirement and charge headlong into making it; but it would have been easier and faster if they took a step back and adjusted their helpers/utilities/services/framework to make incorporating the new feature simpler.

One should stop refactoring once the benefits either don't outweigh the costs or become too hypothetical/far in the future.

Collapse
 
swarupkm profile image
Swarup Kumar Mahapatra

Thanks for the response.
The idea of when to stop refactoring was helpful.
I have seen developers of different experience level, refactor differently.

Hence code review feedbacks too vary.

I have seen the "really old developers" have achieved the right balance/zen of refactoring.

The dangerous ones are intermediate ones who over-engineer because they have recently came accross the topics of refactoring and design patterns and overdo it