image for blog on when to rewrite vs refactor your web app
April 11, 2024

When to Rewrite vs. Refactor Your Web App

PHP Development
Debugging

Managing web applications late in their lifecycle often means devoting more time to fixing issues than developing new features that drive value for your business. And, for companies who aren't ready to sunset the application, making the decision to rewrite vs. refactor their application can be one that is simultaneously complex and time sensitive.

In this blog, we'll walk through why companies rewrite vs. refactor their apps, the benefits and drawbacks of both approaches, and how companies can make the right decision when the time comes.

Back to top

Why Companies Rewrite vs. Refactor Their Web Applications

Refactoring and rewriting code are considered when a legacy code base becomes increasingly difficult and costly to manage and maintain, or when they fail to meet new or emerging needs of the business. Whether you choose refactoring or rewriting ultimately depends on what your goals are and the limitations of your current code base. The decision to do either will share the goal of addressing a few major categories of issues, including maintainability, scalability, technical debt, or a combination of all of the above.

Maintainability

Legacy code is often riddled with patches, poorly or undocumented logic, broken patterns, and difficult to understand code that was written by many people over time. The more difficult your code is to maintain, the longer and more costly it is to introduce a change. As well, changes to difficult to maintain code has a greater likelihood of producing unintended side effects or bugs. Increasing the maintainability of your code will produce long lasting benefits for both code maintenance and adding new features.

Scalability

Beyond maintenance issues, legacy code can also have unique challenges to scaling. Data might be siloed or isolated, old frameworks and libraries might simply not work with your new needs, and the current design pattern simply may not lend itself to efficient scaling. A lot of legacy code can have monolithic properties, lacking the benefits of a microservices approach that breaks up code into loosely coupled services -- with the latter approach making code more scalable and allowing for easier maintenance in the future.

Technical Debt

As the saying goes, "Here be monsters." A living code base is frequently being changed and updated for different reasons. Some of these changes are impromptu, like when a major bug was found and must be fixed, or when cost/timeline doesn't allow for full testing or graceful implementation. These can be necessary things for the business to function — if your production environment is suffering, any fix is better than no fix. However these changes build up and complicate maintainability in the long term. Whether you choose to rewrite vs. refactor your PHP app, both will negate and remove technical debt.

All of the Above

None of the above issues exist in isolation. A code base weighed down with technical debt is also a difficult to maintain code base. Making the decision to rewrite or refactor will address all of these issues in different ways. What’s important is to decide what your goals are before committing to one or the other. That will help you to properly weigh the benefits and costs of rewriting vs. refactoring.

On-Demand Webinar: Modernizing Legacy Web Apps

When a legacy web app becomes obsolete and unsustainable to support, it's time to consider your modernization journey. In this on-demand panel discussion, our experts cover when to refactor vs. rewrite a legacy application, security and compliance considerations, and much more.

Back to top

Refactoring Web Applications

Any code base that exists for long enough will eventually need to be considered for refactoring. Changes to it over time, inconsistent or different coding styles, bug fixes, and many other things will inevitably leave your code in a harder to read and support state than its original incarnation. Refactoring is an important and inevitable part of the software development cycle that will improve your existing code.

What Is Refactoring?

Refactoring is when you rewrite sections or all of the code base to improve its structure, but without changing its basic behavior. The general goal is to improve the code — making it more maintainable and efficient.

Benefits of Refactoring Your Web Application

Refactoring is inherently lower risk than rewriting, since you are working with a known tech stack. You are not changing what your code is doing or introducing new things. Instead the code is being updated it to improve maintainability, perhaps decoupling parts of it to improve scalability, and generally making it more efficient. Some benefits of refactoring code include a smaller resource commitment, improved maintainability and security, and increased overall code performance.

1. Smaller Resource Commitment

Refactoring generally has a smaller time commitment compared to rewriting. It can be done in small chunks as time allows, vs a rewrite which must be done all at once. This allows you to continue making changes and updates to the rest of the code base without concern of having two separate projects to maintain if you were doing a rewrite.

2. Improves Maintainability and Security

Improves the maintainability of your code base. Refactoring can reduce technical debt by making the code more concise and replacing outdated libraries. This will increase the feature velocity in the future. Updating and replacing outdated libraries will not only improve your codes maintainability, but can bring significant security changes and updates as well.

3. Increased Code Performance and Security

Increases performance and efficiency. Refactoring allows you to update algorithms and improve the performance of your code.

Disadvantages of Refactoring Your Web Application

When choosing refactoring over rewriting, the decision should be based off of what your goals are. While often favored over rewriting, refactoring has both limitations in what it can accomplish and pitfalls of its own. It runs the risk of introducing bugs as significant amounts of code are being changed.

1. Inherent Limitations

A basic limitation of refactoring is that it cannot address core architectural issues. A code base written in a no longer maintained framework will still be using that same framework. While refactoring can improve the code base, it cannot necessarily address a lack of best practices and tech advancements.  

2. Difficult to Execute Properly

Refactoring can require a high degree of skill and knowledge to execute properly. It requires a lot of careful study to understand what the code is trying to accomplish so that the outcome is not altered. This can be especially difficult for poorly documented code that has little to no test coverage

3. What Refactoring Is Not

At its core, refactoring improves your code base via increasing its maintainability and efficiency. However, refactoring is not necessarily about adding anything new. A large scale refactoring effort — while greatly improving your code base — does not introduce any new features or options to it.

Back to top

Rewriting Web Applications

To rewrite is to put aside your old set of code and virtually start from scratch. Instead of making incremental improvements to your codebase, you are creating something new. The decision to rewrite is often made when the codebase has become too complex or the underlying architecture is no longer suitable for your needs.

What Constitutes an Application Rewrite?

To perform a successful rewrite, it is common to have two separate teams — one that maintains the old codebase, and one that pursues the rewrite. The old codebase will have a feature lock, only allowing for bug fixes. That way the rewrite isn't chasing a moving target of addition features. Your rewrite team will then start from scratch producing new code that generally emulates what the old program did, but in different ways.

Benefits of Rewriting Your Web Application

Rewriting code can bring benefits that a refactoring does not, though the cost is often higher. This process will allow you to generate something different instead of a replica of the old program. Some benefits include:

1. Improved Infrastructure

You can greatly improve the scalability and underlying infrastructure of your application. Unlike refactoring, the choices of frameworks and other technologies are not limiting factors. This gives you the option to make infrastructure choices more appropriate for the future. These choices can also greatly increase the performance of your codebase, as the new design can be chosen with best practices in mind.

2. Changing the Direction of your Product

A benefit of rewriting a codebase vs. refactoring it is based in what refactoring is not. Instead of making incremental changes, you are starting from scratch. This gives you the freedom to change significant portions of your application, enabling it to pursue new directions or markets.

3. Stronger Security

Additional benefits of rewriting include the elimination of technical debt and security issues. As mentioned, during a rewrite you are free to choose new frameworks and system architecture. This can free you from older decisions that limit your security options. What may have been a secure framework in the past might not be getting any security updates anymore, and rewriting allows you to remedy those foundational issues.

Disadvantages of Rewriting Your Web Application

Rewriting an application is generally thought of as a more risky of an undertaking than refactoring is. Quite often it is advisable to refactor when its a better option. Some of the issues with rewriting include parallel development, introduction of new (and old) issues, and overall costs and risks.

1. Parallel Development

Rewriting is not a fast process, and your business cannot simply be put on pause during it. You will need to split your team so that some are maintaining the old project while others are creating the new one. This can also result in the need for multiple sets of infrastructure, as you will need to support both projects running at the same time for awhile as your transition over.

2. Introduction of New (and Old) Issues 

There is a risk of reintroducing old bugs and losing the background of previous application decisions with a rewrite. Changes and bug fixes in the old codebase might be poorly documented and hence the rewrite team might be unaware of their potential leaving them open to reintroducing old bugs to a given process. As well, any new software will generally have its own issues as well — hence the idea of slowly moving people over to a new system so that there is a chance to troubleshoot it and resolve new issues that were put into it

3. Cost and Risk

Quite simply, cost and time are often detriments to a rewrite. You are eliminating all the work that has been put into the old codebase and starting over. A rewrite will often take longer to accomplish than the original timeline plans for, resulting in additional expenses. This period will also limit your ability to react to the marketplace. While you are rewriting you will not be adding new features to your old codebase. It will need to be feature locked so that the rewrite team isn't chasing a moving target of things to write.

Back to top

When to Rewrite vs. Refactor Your Web Application

When deciding to refactor or rewrite, it is best to consider what your goals are. Each process has different advantages and pitfalls. Refactoring is often viewed as the safer and lower risk option. It can allow you to address technical debt generally obtuse and difficult to support code, making it more maintainable. The timeline available will also feed into your decision to refactor. It can be done a incrementally as time allows.

When choosing to rewrite your codebase over refactoring it, the decision will center around the need to make foundational changes to your codebase. Examples include switching to a new framework, language, or making other significant architectural changes. If your codebase suffers from multiple or inconsistent pattern usage, a rewrite can be a good option to address this. 

Back to top

Final Thoughts

Choosing between a web application rewrite vs. refactor, especially when things are consistently going wrong with your application, can be a high stakes decision. However, it's important for teams to make the right choice for the right reasons. Teams that rush into either approach without understanding the impact of that choice are more likely to feel negative consequences.

Need Help Choosing the Right Path?

Zend can help! With PHP LTS that can keep your legacy applications supported beyond community end of life, and migration and modernization services that can breathe new life into your apps, Zend offers the solutions and hard-earned expertise your team needs. 

See LTS Options   Explore Professional Services 

Additional Resources

Back to top