Refactoring: when it is necessary and what problems it can cause

refaktoryzacja kodu
Refactoring is the process of improving the quality of source code without changing its functionality. In this article, you will learn how refactoring affects a project's quality and stability and the techniques and signals of the need for refactoring. I will also present recommended books on refactoring.

Refactoring is the kind of word that kills conversation when you utter it among friends who are not programmers. It is, after all, nothing more than the process of improving the structure of source code without changing its functionality. This is an essential element in the software development process, as it keeps clean code in good working order and makes it easier to develop later.

During refactoring:

  • you remove the unnecessary code
  • you simplify complex structures
  • you improve the readability of your code
  • You increase its transparency.

All this is to make the code easier to maintain and less error-prone.

Importantly, refactoring is not about adding new functionality but improving existing code. Keeping the code more organized makes it easier to make changes and catch errors, which translates into the overall quality of the code.

Pros of refactoring

  • Keeping the code in good working order
  • Facilitate subsequent software development
  • Removal of unnecessary code
  • Simplifying complex structures
  • Improving code readability
  • Increasing its transparency
  • Easier to make changes
  • Catching errors, which translates into the quality of all software

What is the expected result of code refactoring?

The result of refactoring is code that is in a better state than before. This translates into easier future changes and catching errors, which translates into the quality of the overall software.

Refactoring also improves the readability of the code and makes it easier to understand. Refactoring often also results in removing a large amount of unnecessary code.

What problems can be encountered during refactoring?

Can code refactoring increase the number of problems?
Can code refactoring increase the number of problems?

When refactoring your code, you may encounter many different problems. One of the most common is the risk of introducing new bugs when changing the code. Another problem may be the need for additional testing to ensure that changes have not affected the software’s functionality. Refactoring can also take time and resources, especially on larger projects.

Time-consuming

Code refactoring is a process that aims to improve the quality of code by making changes that will increase readability, understandability and maintainability. This process can take a lot of time and resources, especially for larger projects. Therefore, it is crucial to carry out this code refactoring carefully and without going crazy. To this end, you might consider creating a refactoring plan that includes prioritizing the areas of code that need the most attention and ensuring that the refactoring process is gradual and controlled.

The possibility of breaking something that was working well

There is a risk of introducing new errors when changing the code. Therefore, any change must be thoroughly tested. Ideally, the code is covered with automated tests. Refactoring is also a good time to add missing tests. Moreover, in addition to testing, the code must be carefully documented to make it easier for other team members to understand in the future. Also, make sure that the code is written in a clear and readable manner, making it easier to further develop and maintain in the future.

The need for testing

Every code change carries some risk of introducing new bugs. To reduce this risk, conducting testing after each change is necessary, especially if it involves introducing new functionality or modifying existing code.

This is incredibly difficult when you rely on manual regression testing. That’s when the risk is greatest so you have to be very careful.

When refactoring, having other programmers review the code regularly is a good idea, allowing you to detect errors and improve code quality quickly. When refactoring, having other programmers review the code regularly is a good idea, allowing you to detect errors and improve code quality quickly. Therefore, it makes sense to approach the process with due care and the understanding that every change is a potential opportunity to develop and improve the project.

Refactoring the code may require additional testing to ensure that the changes have not negatively affected the software’s functionality. At the same time, it is a risk that is often worth taking to improve the quality of the project.

Costs

Refactoring and improving code quality can be costly and resource-intensive, especially for larger projects. However, it is worth investing in this activity, as it brings many benefits.

Improving code quality greatly facilitates further project development, increases its stability and reliability, and makes it easier to work on in the future. In addition, refactoring allows more efficient use of resources and increases code readability, making it easier for developers and speeding up implementing of new features and functionality.

Therefore, spending time and resources on program refactoring is worthwhile to achieve better code quality and streamline project development processes.

The cost of refactoring depends on several factors, such as the project’s size, the code’s complexity, the number of developers working on the project and many others.

Risks

Refactoring code or making a series of changes to source code can be a complicated and time-consuming procedure that requires knowledge and experience. Without proper preparation and skills, such a change could increase the risk of errors, leading to side effects such as performance drops or malfunctioning applications. Therefore, it is vital to have experienced programmers who can either refactor or mentor less experienced colleagues.

5 signals indicating that an application needs refactoring

Technical debt in the code makes the project like an abandoned city - programmers flee to a new project.
Technical debt in the code makes the project like an abandoned city – programmers flee to a new project.

When code becomes difficult to understand, modify or test, this is a signal that may indicate the need for refactoring. When making changes to a system (even the simplest ones) is a nightmare, it may be the last call to do refactoring. Indeed, as a good programmer, you will often notice the need to refactor your code.

Teams that don’t do refactoring quickly fall apart because, in our industry, a programmer can change jobs like gloves, and when the daily code work is overwhelming, and there is no room for improvement, then changing jobs is an easy and effective option.

If the code is inconsistent or unintuitive, it will be difficult for new programmers to understand how it works. Related to this is that it’s hard to find developers for teams that maintain some very smelly code that squeezes out tears just from looking at it.

Code is difficult to understand, modify or test

Correctly understanding the code can help you avoid errors and improve your work. Collaborating with other developers can also help you understand and improve your code in ways that save time and increase productivity.

Sometimes, however, the code is written so that it is impossible to understand. There are no tests or documentation and the programmer who wrote it is already working for another company. This is a clear signal that it is time to rewrite this code.

Code is written in an inconsistent or unintuitive manner.

Inconsistent code is hard to understand. Even the smallest piece of code can be complex for others to understand. To improve its readability, rethink its structure, ensure consistent naming conventions, better code readability and use more intuitive syntax. You might also consider adding comments to make it easier for other programmers to understand how it works. This will make it more accessible and allow others to work on the project in the future.

The code contains repeated fragments

Code that repeats the same fragments can make it difficult to maintain code in the future. In this case, when existing code contains repetitive fragments, consider using functions or variables to help minimize code duplication and facilitate future development.

Code causes problems when changes are made

When you make changes in one place and the application explodes in another, this is a good enough reason to start refactoring and getting rid of technology debt. Developers must know the code they are creating and can modify it effectively. In this way, you can avoid errors and ensure that the application works correctly and without interference.

Refactoring techniques

Can you see the technical debt? Act as soon as possible and start rewriting the code for the better!
Can you see the technical debt? Act as soon as possible and start rewriting the code for the better!

Martin Fowler in his book “Refactoring” describes many excellent refactoring techniques. Let me show you some of them here.

Feature extraction

Function extraction is a refactoring technique that involves extracting a specific portion of code from a method and placing it in a separate method. I really like this technique because it allows you to separate some code into a function (it can even be about two lines of code) and call that code by naming the function. Thanks to the refactored function, this improves readability and makes the code usable elsewhere.

Example:

Before refactoring:

function calculateOrderTotal(price, tax_rate, discount) {
    const taxAmount = price * tax_rate
    const discountAmount = price * discount
    const totalAmount = price + taxAmount - discountAmount

    return totalAmount
}

After refactoring:

function calculateOrderTotal(price, taxRate, discount) {
    const taxAmount = calculateTax(price, taxRate)
    const discountAmount = calculateDiscount(price, discount)
    const totalAmount = price + taxAmount - discountAmount

    return totalAmount
}

function calculateTax(price, taxRate) {
    return price * taxRate
}

function calculateDiscount(price, discount) {
    return price * discount
}

Absorption of function

Function absorption is a refactoring technique that involves replacing a function call with code in the function’s body. This is often used when a function is called only in one place in the code and is not used anywhere else. By absorbing functions, the code becomes more readable and easier to understand.

Example:

function calculateSum(a, b) {
    return a + b;
}

function calculateDifference(a, b) {
    return a - b;
}

function calculate(a, b) {
    console.log(calculateSum(a, b));
    console.log(calculateDifference(a, b))
}

In this case, you can use function absorption and replace the function calls with the code found in the body of the calculate function:

function calculate(a, b) {
    console.log(a + b);
    console.log(a -b)
}

This makes the code more readable and easier to understand, and in case of code changes, you don’t have to change many functions, just one.

Gathering functions into a class

Gathering functions into a class is a refactoring technique that involves placing functions inside a class. This can make the code more readable, more manageable and more modular.

Example:

Before refactoring:

function calculateSum(a, b) {
    return a + b;
}

function calculateDifference(a, b) {
    return a - b;
}

After refactoring:

class Calculator {
    calculateSum(a, b) {
        return a + b;
    }

    calculateDifference(a, b) {
        return a - b;
    }
}

By gathering the functions into a class, you can more easily manage the code and avoid problems with variable coverage. In addition, the code may be easier to test and debug, and changes may be easier to make in the future.

Breakdown of the loop

Loop splitting is a refactoring technique that involves splitting an existing loop into two or more loops to make the code easier to understand and more readable. This can be useful when the loop performs several tasks simultaneously or has too many conditions.

Before refactoring:

let averageAge = 0;
let totalSalary = 0;

for (const person of people) {
    averageAge += person.age;
    totalSalary += person.salary
}

averageAge = averageAge / people.length

After refactoring:

let totalSalary = 0;
for (const person of people) {
    totalSalary += person.salary
}

let averageAge = 0;
for (const person of people) {
    averageAge += person.age;
}

averageAge = averageAge / people.length

In this case, the original loop was split into two loops: one that counts total earnings and another that calculates average age. This makes the code more readable and easier to understand, and any changes can be easier to make in the future.

Summary

Refactoring is the process of improving source code structure that does not change its functionality. It removes redundant code, simplifies complex structures and increases code clarity. This is essential in the software development process, as it keeps the code in good working order and makes it easier to develop. In this article, I showed you refactoring techniques, the signals of the need for refactoring, and the pros and cons of the process.

Cost savings is not a good reason to avoid refactoring
Cost savings is not a good reason to avoid refactoring

If you want to delve into the topic of code refactoring then I recommend these books:

  • “Refactoring” by Martin Fowler
  • “Clean Code” by Robert C. Martin
  • “Cod complete” by Steve McConnell.
Share the Post:

You might also like

A career in technology: How to develop your skills

Are you a programmer and would like to grow? The Internet is full of materials on how to do this. Despite this, don’t run away – I have something that will interest you. Did you know that Adam Malysz, the legendary Polish ski jumper, was a roofer before he became a flying champion? I dare not compare myself with Mr. Adam, while there are two things we have in common.

I was also a roofer and also managed to re-brand myself. Maybe not in such a spectacular way, but still. In this article, I’ll share my personal experience on my journey from roofer to programmer to tech lead and give you tips you can apply to grow and advance and maybe even dramatically change your career.

Read More
AHA! Let's bring back the simplicity of the Frontend

AHA! Let’s bring back the simplicity of Frontend

Have you wondered why, in this day and age, when we have access to the latest technologies and solutions, IT projects still fail? Don’t you think that we complicate our lives and work in many cases instead of simplifying it? Sometimes less is more, especially in the world of frontend! Read on to learn what an AHA stack is and how to do frontend more simply.

Read More