IT projects are born and grow in different ways. Some are planned from start to finish, and the plan really is followed all along. In far more cases the team runs into twists and turns on the way, such as a deadline that is getting too close, requirements changed "on the fly" or a case that was not considered when the architecture was planned. In such situations, though, regular refactoring usually helps, straightening out the code in calmer times. Unfortunately, we all know there are also projects where various events lead to a point at which, after some time, developers talk more and more boldly about rewriting the project from scratch. The project manager answers just as boldly, "no way".

How can you tell that software really needs to be rewritten, and when it is only the whims and frustration of developers? Are there situations at all where, after many years, the code really is a candidate for being redesigned and built from the beginning? We will try to answer the first question below, but we can answer the second right away: yes, such cases certainly exist. Still, only a small percentage of them will actually get a second implementation, and as you probably guess, the reasons lie in the business.

What makes a project need a restart?

Many people will probably answer that ugly code full of bad smells is reason enough. That is not true. Not every nasty piece of code justifies rewriting the whole software from scratch. At least not under the business conditions that you have to take into account in your plans, and that are the hardest to explain to the so-called technology gunslingers, who are the first to call for a restart of the project because it is simply fun. And yes, I admit it: designing an architecture and starting something from scratch, hoping for a better future, really is fun, but it is not necessarily the right solution.

First, you need to identify where the problems with the code really are. At this stage it often turns out that they affect only specific parts of the software or show up only at certain moments. In that case there is no point in rewriting everything. It is much easier to extract a specific part into a separate module or even a sub-application and refactor it. This also has to be done with care, but more on that later. The situation changes when the analysis shows that the problem lies in the very foundations, for example in how the key objects managed by the system are handled. In such a setup, small changes here and there are much harder.

Second, you need to review the technology stack used in the software. The most common real reason for rewriting systems is legacy code built on library versions that are archaic or simply no longer supported. An example is an application that was created in Angular 2 and has not been updated since (as I write this, version 16 is available). In that case it is no surprise that developers complain about working on software in such an old technology. It is also worth asking whether updating the version is really problematic. Maybe it will not be so bad and you only need to adjust a few things. Although I have to admit that sometimes it is a trial by fire, especially in frameworks whose authors openly drop so-called backward compatibility.

Third, you need to consider the recent changes and the direction of the client's new requirements. Did they really make something that would take a few days in a "normal" architecture take a few weeks here and generate many previously unknown problems? The question is not whether it could have been done faster, but whether the cause of the delay was problems with the existing code. If so, and the situation keeps repeating, it becomes a solid argument for rewriting a large part of the system. It is also worth analyzing whether the changes the client requests really make sense and are necessary. Maybe the same business goals can be reached in a different way, or the current direction even contradicts the initial assumptions. These are problems to be settled at the business and management level, and sometimes they even mean a second application.

And finally, fourth, you have to confront all this with the business environment. If, despite its problems, the project has to keep developing and the client gets benefits from it that the software house benefits from too, then suddenly saying "stop" and starting work from scratch is not the best way out. It is a battle of compromises. On the one hand, you cannot stop making changes, and on the other, pushing on with bad code only deepens the problem and the technical debt. As if that were not enough, there is hardly any way to combine development and a restart of the project: the first has to stop so that the second can proceed effectively. Fortunately, there is a way to speed this up.

How to carry out a restart and a larger refactoring

I deliberately used the term "larger refactoring", because even under favorable circumstances, building the project from scratch is still not always a good idea. I will go further: it is usually unfeasible in such a pure form, unless the team has plenty of resources and time and does not have to hurry anywhere (for example, it is the market leader in its area). A much more popular approach is to transform the existing software gradually, step by step. But you have to prepare for that.

First of all, an analysis of the real problems, which the developers prepare before making a decision, is useful. There are no projects where everything is bad. There are ones where some areas are done so badly that they affect others that are potentially "normal". Maybe in an application for managing a stable, the problem is not handling horses or the building, but the user permission system, because of which every new screen means double the work? Or maybe wrong assumptions were made about introducing an API, with an unsuitable technology chosen along the way? It is worth going through the reports of time spent on given requirements once more and finding those that keep causing delays. Such an analysis is needed to decide what will actually change.

The second stage is making the decision and planning the direction of the changes, that is, thinking through the architectural changes and singling out the part that will be covered. Some may say this is a good moment to update the framework and libraries, but I would personally advise holding off for a moment and noticing that the approach could be completely different. Maybe the technology used in the project is not supported at all, or will not allow real changes? Some of us have run into similar situations, for example many years ago, when the standard Java + GWT combination used for admin panels turned out to be an ever bigger burden for several reasons. Instead, we simplified the approach and started using the mechanisms provided first by Yii 1.0 and later Yii 2.0. That sped up the work significantly and also let more people work on such projects in the future. If, however, we decide that the current technologies are good and only need an update, then this really is a good moment. At this stage no architectural changes are made yet, only the code is adapted to the new version of the libraries. Unless that in itself means a major modification of the structures in use.

The third stage is setting the order of the changes. Doing everything at once is a terrible idea (unless we are really talking about a brand new project). You have to approach it sensibly. If some matters can be changed in parallel and do not affect each other, you can be tempted to do so, but it is worth considering whether it is possible to extract a module that will be rewritten from scratch. And then the next one, and so on. Yes, this is where modular and microservice approaches shine, because they separate individual areas of the software and make changes easier. Sometimes this comes naturally, and sometimes you have to try a bit. An undeniable advantage is that such a division naturally stays and will make planning work on given parts easier in the future.

In particular, it is worth considering extracting modules that will be available to the rest of the software through an API (and not directly in code, as until now), and then writing a new module alongside, which will replace the old one in the future. The advantage is that you then simply need to switch the old part over to the new one. In the end, for some time both versions can run side by side and be compared against different criteria. Of course, this is hard to consider when the problems with the application are fundamental and concern elements that cannot be extracted. But to be honest, that is a rather rare situation.

Another advantage of the step-by-step approach is that along the way it may turn out that the full change (or refactoring)... is not needed. Maybe after rewriting 30% of the software it turns out that the system already works as it should and the most important problems are solved? Of course, this requires the solid analysis I wrote about above. It is a condition without which the new code will not be significantly better than the old, or at least such an effect would be more a matter of luck than reason. But when everything is done "honestly", there is a chance that not everything will be "reset", and the team and the client will be able to develop the software gradually, possibly with further architectural changes in parallel.

One thing is worth mentioning: make sure that writing the new part of the software does not introduce new bugs. In such a setup, it is worth thinking about automated tests or at least another approach that verifies the correctness of the transformation.

Summary

Of course, sometimes there is no way around it and the software has to be rewritten. Still, in many situations a solid, well-analyzed refactoring turns out to be the better way out. Maybe the problem is only the frontend, not the backend, and you can rewrite just half of the application? These decisions depend on the specific project, but remember that every attempt to build software from scratch is a big undertaking that requires resources and a time buffer. So, as the classic says, prevention is better than cure, and it is better to try to create good-quality software from the start. Not necessarily perfect, but good enough to satisfy the schedule, the budget and the development all at once.

Best regards and thank you, Jakub Rojek.