Author Marko Krstic
What is code refactoring
We often hear from developers they need to refactor the codebase. They explain to us it's essential for our software.
They talk about clean, nicely structured, and organized code all the time. But is it necessary to do it? Why do developers want to do it, and should we spend time and money on it?
Before we answer these questions, we need to understand what code refactoring is.
What is code refactoring?
As we know, software development is a process of writing code by humans in a particular programming language. Each programming language has its own rules and ways to write code to produce a software solution. One solution can be literally written in many ways. If we found 100 developers and gave them the same, not trivial task (i.e., to build a web application), we would get 100 different ways of the same solution that does the same thing.
There is no standard way or path to get to a particular software solution in software engineering. The same software might be written in many different ways and still has the same behavior.
To build software, we use programming languages to write small units of code or small building blocks that we can combine and make an entire solution. We can wire these tiny building blocks in different ways to produce the same result. These are small functions that do simple things.
We can think of the building blocks as bricks/pipes/tiles when we need to build a house. All bricks/pipes/tiles are the same or similar and relatively small. Each of them means nothing by itself, but putting them together, produces a house.
The way we combine them is called the code structure of our software. We can employ them and incorporate them differently and still get the same behavior.
Therefore, let's see what code refactoring is.
Refactoring or code refactoring is a technique in software engineering for alerting an existing code structure without changing the software's behavior.
To put it in other words, we are changing the code structure, moving around and uniting our small building blocks in different ways, without changing what our software is doing, without changing its behavior.
Why do we need refactoring?
It is important to note that our software will do the same thing as before; it will not be faster (usually, but sometimes it can), it will not be prettier, it will not have more features, and it will not attract more customers after code refactoring. Moreover, no one cannot even say if it's refactored or not other than our developers.
When we know all of these, we have a full right to ask why we need to do it? Why spend money and time on something that no one can even notice except our developers?
Let's take another example of a local pharmacy, and let's imagine a situation in which we have a separate room in our shop where we put all our medicines. That room is neither accessible nor visible to our customers.
Since it's not visible to anyone except the people who work there, we could put all medicines on the same pile without any order. We can create a complete mess in our storage room, and none of our customers would ever even notice that.
Another way of organizing it would be to put everything on the shelves and drawers where each medicine stays in its designated place. If we managed it this way (where everything is in its place), would our customers notice it? No. Would our pharmacy look prettier for our customers? No. Would the staff be more polite to our customers? No. Would our medicines be more expensive or cheaper? No. Is there anything our customers might notice regarding this? No.
If so, then why is it essential in which way we store our medicines?
Since a local pharmacy is not the same as building software, we can immediately see the benefits of making our storage room clean, organized, and well-structured even though our customers can not see it.
We are doing it because it might affect our business. For example, the waiting list in front of the check desk might be long since we need to find adequate drugs for our customers. Our customers might wait for a few minutes before we find what we look for in that mess of a huge pile. If everything stays well-organized, we can find it in terms of seconds. If the waiting list is too long, we will have unhappy customers, and many will not return again. That can hurt our business.
Cleaning the storage room and creating the order we call refactoring. No one can see how it looks except the employees. The same rules apply to software development. The only difference is that the reasons for the software code refactoring are not as obvious as for the local pharmacy.
We can also have a tremendous mess in our code in terms of organization and code structure, which can hurt our business in many different ways.
First, adding new features on top of the not well-structured code is not that straightforward. Implementing new functionality can take a while, and we cannot do it fast enough. The more mess we have in our code, the longer it will take to develop anything new. Since it affects the development time, it indirectly affects the costs as well.
Not even a not-structured code can slow down the development, but in some cases, it can be so bad that we can hit the wall and cannot implement any new features. It is a nightmare for any developer working on such software. Two possible things to do to solve it: do a massive refactoring (it could take a lot of time) or re-write the software from scratch. Both options are costly, and they can often wholly ruin the business.
Harmful code is not only related to the duration of adding new features, but also it is usually very error-prone. Changing anything in such a codebase is very risky, and the odds of introducing new errors and bugs are pretty high. No one likes buggy software, especially clients who use and pay for it.
In a codebase that is not well organized and refactored, it might happen that we unconsciously introduce a bug into a separate and unrelated feature by changing something else. It's usually not easy to find these kinds of bugs, and almost always, they are shipped to production.
We know that well-written software has many automated tests to test the application functionality and ensure that everything works as expected. It can be challenging to implement such tests in the messy codebase, and in some cases, it would even be an impossible mission. In such an environment, developers need to dedicate a lot of time to do huge refactoring or skip adding automated tests. The first might be extremely expensive and time-consuming, and the second is like shooting in your legs.
To prevent all these things, we need to do refactoring regularly and keep our codebase well designed and structured. We should avoid massive refactoring and favor the small ones. That way, we can make sure we don't break our software and not introduce new bugs. We should never wait too long without refactoring; otherwise, it might become super costly and dangerous for our business.