DEV Community

Joanna Otmianowska
Joanna Otmianowska

Posted on


To refactor or not to refactor?

While working with code (especially legacy one), you may often stumble upon parts of the code that needs refactoring. But... should you always refactor?

You may say - where this question is coming form? Just refactor. There is a Scout rule! Always leave the code in the better shape.

That's true. But...

It's good to leave code in a better shape that it was before. However, refactoring (especially in complex, legacy projects) may be time consuming and may not add a clear benefit to your current work

Before refactoring, consider things below:

  • How complex is the change I want to make?
  • Why I want to make this change? Will other devs benefit from it (or do I only want to rewrite the code in my way)?
  • Does current task benefit from this change? If so, how?
  • Would this change slow down the development?
  • What is testing scope for that? Is there any additional regression testing needed after this change is implemented?

There are also small refactoring tasks you can usually do with no major impact, these are:

  • Code formatting so the code is more readable and follows current project standard
  • Change variables or functions names to be more descriptive
  • Extracting code to variables/functions if you see that code is being duplicated
  • Creating helper functions that can be reused

And do you always refactor?

Top comments (0)

Advice For Junior Developers

Advice from a career of 15+ years for new and beginner developers just getting started on their journey.