Code Refactoring vs Rewriting
We have an application which is punctured with technical debt, out of date, or undeserving for users so what to do with such code – should we continue to do incremental refactoring? Or scrap all and rewrite from scratch? Any legacy application which needs improvement and what to do next is not a simple decision. We plan to keep backend services as they are but upgrade front end should we refactor or rewrite? What about if we want to break a monolith into different microservices? Business users need to understand what type of effort is involved to better set expectations.
Today we look more in detail about two important terminologies in the software domain – code refactoring and code rewriting. This article will give insight into the major differences between the two, its purpose , which one to choose from and so on.
What is Code Refactoring?
Functional enhancements is a key start point and to decide what rewrites and refactors are not. Which strategies will improve functionality of an application? Fixing defects, delivering new features, or cleaning up of user interface is call of enhancement. It is all about what an application does for users and a normal state of development. Sometimes the scope of functional enhancements is large. Like having an app server which is user based but features all require overhauling. This kind of effort requires building all new functionality , a new system required to be developed from scratch.
Refactoring is a discipline for restructuring an existing body of code , altering its internal structure keeping external appearance intact. Refactoring is all about making code maintainable by decomposing long or complex functions, fixing inconsistent naming conventions, adding unit tests, class hierarchies restructuring, data structures or schema revamp. There is nothing visible to users but internal code structure is modified to make it easy to work for developers and hence improves productivity. Refactoring is an iterative change and makes minor tweaks to application, deliver them and then rinse and repeat.
Benefits of Code Refactoring
- Easy to start as developers already working on code and know it well
- Ideal for all kind of software architecture be it monolithic or modular
- Adds more flexibility as developer can choose to fix selected segments only
- It sticks to one code base and there is no need to create two code bases hence maintenance costs are low
What is Code Rewriting?
Rewriting has a simple and basic goal to improve the non-functional nature of the application. It is about blowing up and building new. Rewrite involves major changes to the system in order to make fundamental improvements in the quality attributes of an application. If a legacy application is crippled due to technical debt it is impossible to add new features, then we may choose to rewrite and build a new base to improve maintainability and extensibility and add in new features which business demands. So, it is being rewritten with enhancements.
Rewriting is building the same functionality which exists in legacy applications but using a different coding language / framework, maintain new code repository and deploy and entire new platform e.g., servers, hardware, server less, client etc.
Benefits of Rewriting code
- It has wide range of possibilities as you are not limited to use previous structure .
- Acquisitions are easier as people who developed legacy code are not there it is better to have code rewritten.
- More future oriented as you will be able to avoid mistakes already identified in legacy systems and recreate everything from scratch.
The below diagram represents the blend of major and minor changes. So, the re-implementation is proposed with a modern set of technologies , but keeping the exposed APIs and underlying structure same.
Comparison Table: Code Refactoring vs Rewriting
|Definition||Restructuring existing code without impacting its external interface||Re-implementing a large code of existing functionality without re-use of its source code or writing inscription|
|Costs||There is no need to maintain two code bases hence costs are lower||There is a need to maintain two code bases both the new and old one and this generates additional costs|
|Characteristics||Iterations are fast
Same underlying technology / architecture
Requires a lot of expertise as developers need to deal with complex patterns and ambiguities
|Long term commitment
Freedom to choose new framework or programming language
|Effort estimation||The effort required to refactor is less provided developers are well aware of legacy application code||Creating new systems from scratch takes a lot of time|
|Examples||Extraction of variable, renaming a function, consolidating statements or moving code from one place to another etc.||New technology to do same thing in better way, replacement of a legacy that is difficult to work with (e.g., complex methods without tests) etc.|
Download the comparison table: Refactor vs Rewrite
The usual standard is 40% refactor and 60% rewrite. Do you refactor or rewrite, would depend on a few crucial factors as under ?
Your obligations towards your investors – if you are bound to pay back some investments then these can greatly influence how you’re looking at the code.
UI/UX designs – If there are major changes from version to version. It is better and easier to start from scratch rather than refactor the current codebase.
Environment changes – want to migrate from one hosting solution to another, you may find some issues in a full exchange resulting in the requirement of refactoring or rewrite.
How microservices architecture is changing technology paradigm
Introduction to Postman: API Development Platform