The activity is often unclear to those who do not work in the sector, today we try to clarify this topic and explain what its goal is.

What is refactoring?

Refactoring is the activity of rewriting the existing code making it clearer while keeping the functionality unchanged.
Already from this definition we can understand an important thing:
refactoring does not solve bugs or at least this is not its purpose.
During development, the refactoring activity serves to simplify and make your work more readable.
It often happens that after a while the code is problematic and cryptic, this could be due to requests for fixes or features or simply when:
“Who is the fool who wrote this code?”
“…..It’s me”
I realize that for some people it may sound a little strange but the programmer’s work is an everlasting study. As a matter of fact every time we understand that we can make it better, it means that we are improving.

Why refactoring is so important?

Cost reduction

The time used to refactor is an investment in making subsequent code changes faster.
A readable code is easier to understand and therefore to modify. If the code is never refactored, each new feature will take longer and longer to be written, thus also increasing development costs.

Simplicity of onboarding for new team members

Increasing the number of components does not necessarily mean that the development speed increases.
There are several causes for this problem including the one that a project may have an unclear codebase.

bug research

Even if the goal of refactoring is not to remove bugs, simplifying the code, we realize very often that it did not really do what we expected.

When to do refactoring?

Cartoon without refactoring

Unfortunately in most legacy projects the tests are few or absent. 
A good practice could be to separate the code we want to modify and insert the tests only in that specific section before starting with the refactoring.

In general it is better to do the refactoring, every time you work on a task and the code we are going to modify is unclear.

You must always remember that although the code can always be improved at some point, you must stop and avoid spending more time than necessary to rewrite the code.