Why You Shouldn’t Be Afraid of Refactoring

Erez Carmel
4 min readApr 28, 2022

It can be scary to refactor code, even if you know it’s necessary and that you have the skills to do it. But there are some good reasons why web developers shouldn’t be afraid of refactoring, including better code organization, easier error correction, and an easier time finding resources when you need them most. So if refactoring has so many benefits, why are we so afraid to do it?

Spaghetti

As web developers, we’re scared to touch code once it works. We get stuck on good enough, and before you know it our codebase is a mess of spaghetti and mud. Good news: that doesn’t have to be you! Let’s overcome our fear of refactoring and keep a clean, readable codebase that anyone can work with.

When it comes to writing software, you should constantly strive to clean up your code. As you refactor your codebase, you’ll make it easier for other developers to get on board and contribute without a massive learning curve. The sooner you start thinking about other developers using your code, the better! They’ll thank you later — and they’ll probably be able to write more quality code faster as well.

Why doing it???

There are a few key reasons why we need to refactor, but there’s one good one that applies in just about every situation: refactoring makes your code better. It doesn’t have to be better in terms of popularity or performance or efficiency; it can be better because it is easier to read, more logical, and more consistent. When you think about how much time developers spend making a piece of software perform efficiently — focusing on milliseconds when building interactive applications — it seems foolish not to take advantage of tools that make those same pieces easier to understand for years after they’ve been completed. If you’re going to spend all that time writing code, wouldn’t it be nice if someone else could easily figure out what it does?

Code, push, deploy, refactor, repeat.

Having a good sense of when and how to refactor your code is an important skill for developers. It’s easy to fall into a trap, where you’re constantly adding new features without removing or improving existing ones, which leads to a buildup of technical debt. Remember that code needs maintenance too; even something as simple as renaming a class takes effort, especially if you’ve got other developers touching it in parallel with you. If they have to stop what they’re doing and look up what something means because you forgot to document it properly before changing it, your team could be held back by minutes or hours each time — hours that could add up to days over time!

This is the reason why the development cycle we all know:

  1. Code
  2. Push
  3. Deploy

must include a new step:

4. Refactor

Every step can be included multiple times until you are satisfied with your code. When there is some resistance to refactoring, it’s because developers have a bad feeling about refactoring their code. But don’t forget that there might be others working on your code at some time and they will appreciate an extra layer of documentation: a comment right next to your change explaining what it does or why you made it will prevent them from having to dig through version control history or save a couple of lines as an inline comment. It also prevents future bugs in your code since other developers won’t have to spend time figuring out what you were thinking when you wrote it. So keep in mind that every line of code needs maintenance too!

The more complex a system gets, the more important good design becomes. The challenge lies in knowing when to stop adding features and start improving existing ones instead — and how much work is required for each one. This is where refactoring comes into play; it helps us take stock of our current state and decide which parts need improvement most urgently. That’s why you should never be afraid of refactoring your code!

The more complex a system gets, the more important good design becomes. The challenge lies in knowing when to stop adding features and start improving existing ones instead — and how much work is required for each one. This is where refactoring comes into play; it helps us take stock of our current state and decide which parts need improvement most urgently. That’s why you should never be afraid of refactoring your code!

What’s next?

If you’re not sure where to start refactoring your code, a good first step is to write unit tests to ensure your code stays in working order. Although some consider refactoring to be a second-class activity that ought not to take place while building new features, refactoring actually saves time in testing and debugging code because it ensures existing functionality isn’t broken by any changes. This can help you avoid future development delays and cost overruns when building out new systems.

The next steps to start refactoring your code could include:

  • Identify sections of code with duplicated or similar functionality
  • Move all common elements into a shared function
  • Test all modified areas for errors

Now go refactor your code!

That’s all for today, let me know what you think.

--

--

Erez Carmel

Full stack developer, consultant & lecturer, experienced with developing interfaces on various platforms.