What is Refactoring?
Tags: webdev, programming, javascript, codequality
Published: 2021-01-25 03:23:06

**Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62**

**Subscribe to my email list now at http://jauyeung.net/subscribe/**

Refactoring is something that we often have to do when developing software. Therefore, we’ve to know about it.

In this article, we’ll look at the definition of refactoring what we have to do to refactor our code.

### Definition of Refactoring

Refactoring is a change made to the structure of our software so that it makes it easier to understand and modify without changing its observable behavior.

There’re many ways to refactor our code to make them cleaner and easy to change.

To refactor is to change software by applying one or more refactoring without changing the software’s observable behavior.

It’s pretty much just cleaning up code to make our lives easier.

After refactoring, our code should be easier to understand and work with.

We can make changes in the software that make little to no change in behavior while just making them easier to work with.

### Wearing Two Hats

We can measure the progress of refactoring by adding tests to see if our refactoring changed anything unintentionally.

We should be both adding tests if they don’t exist yet and changing our production code.

Once we have the tests then we just have to run them to see if our refactoring changed any behavior.

All we have to do to make sure that we didn’t change anything is to run the tests.

### Why Should We Refactor?

Refactoring makes our lives easier after the refactoring id done.

It improves the design of our software. Changes may be made without thinking fully about the design.

Therefore, we can clean up the code so that the code is designed better.

Poorly designed code takes more code to do the same things. The code might be doing the same thing in several places.

We can eliminate these issues by moving the common code to one place and referencing that.

This reduces duplication and code bloat. The less code is there, the less code that we’ve to modify.

The more code there is, the harder it is to modify. There’s more to understand and we may run into issues like the changes we made didn’t do what we expect because they're places that we didn’t change but we’re supposed to.

If we remove the duplicates, then there’s only one place to change to get what we want.

### Refactoring Makes Software Easier to Understand

Many people have to read code. We probably read more code than we write, so we should make our lives easier by refactoring our code so that we can understand them easier.

More often than not, people may just get something to work and then forget about.

Refactoring can make code that’s done quickly to just make things work easier to understand.

A bit of time spent on refactoring can communicate what the code is doing better.

Programming is all about saying what we’re doing.

It also helps us work faster since we can understand clean code easier than messy code.

As the code gets clearer, we can find things that we didn’t see before.

If we didn’t change the code, we may miss those things.

### Refactoring Helps Find Bugs

Refactoring may make bugs surface. The new understanding that we get by refactoring may find flaws in logic that we may have missed before.

![](https://cdn-images-1.medium.com/max/800/0*oE046bzMFVOMYG_R)Photo by [Harley-Davidson](https://unsplash.com/@harleydavidson?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral)

### Refactoring Helps Us Program Faster

This is the main purpose of refactoring. If the code is cleaner, then it’ll help us work faster.

If there’s less code bloat and duplicated bloat, then we can drill down to where we need to make our changes easier than if we left them messy.

Good design is critical for fast software development. The point of having a good design is to make development faster.

Without good design, the code deteriorates and making changes will get much harder fast.

Refactoring helps us develop software more quickly since it stops the design from decaying.

### When Should We Refactor?

We should refactor when we see something duplicated 3 times. Once we have it 3 times, we refactor.

When we add functionality, we should also refactor to make adding the functionality easier.

Also, when we fix bugs, we should aim to clean up the code as we fix bugs to make everyone’s lives easier later on.

Code reviews are also a great chance to suggest refactoring before they get merged into the main branch.

### Conclusion

Refactoring is cleaning up our code. It makes it easier to understand and change so that we can develop faster.

We should refactor when we’re changing code. Pretty much any situation when we’ve to change code is a good time to refactor.