Refactor or Rewrite?
The goal of refactoring and rewriting is, to improve the sanity of the system by improving the code readability, structure and clarity. A clean code would be easier to maintain and enhance. However, on many occasions Agile teams have a tough time deciding between the two.
Michael Dubakov suggested the following reasons for the code base getting worse over time,
- More and more features. It leads to increased complexity.
- Shortcuts and hacks to support “We need this fancy search till August. Period!” features
- Developers rotation. New developers don’t know all the fundamental decisions and ideas behind the architecture. Knowledge gets lost with transition inevitably.
- Development team growth. More people - less communication. Less communication - bad decisions.
Michael suggested that, though refactoring and rewrite might lead to cleaner code however, both the techniques bring chaos to the existing system. Refactoring is an incremental activity hence it touches some portion of the system at a time. This creates chaos at local levels and might be easy to contain. Rewrite on the other hand is a more invasive change and results in a bigger chaos in the system. Due to the wider impact, the stabilization period of a rewrite is much longer than that of refactoring.
We have the old system during rewrite, so chaos is constant. After the public release chaos increases significantly. Quite many new (and old) bugs and quirks are expected, so stabilization period is longer.
Peter Schuh suggested that often teams use the words interchangeably there by resulting in more confusion and chaos. Teams should understand that rewriting is riskier proposition as compared to refactoring and hence should use the terminology accordingly. According to him,
It’s just semantics. Well … it’s only semantics until someone gets hurt. Rewriting code is a risky and sometimes painful endeavor. It doesn’t always end well. If we execute a rewrite but call it a refactor and the whole thing goes pear-shaped, no business person is going to stop and think about semantics. They’re just going to cringe the next time they hear the word refactor.
Guido A.J. Stevens made an interesting observation. He suggested that the question is not between refactoring and rewriting but it is between either: refactor, or else: rewrite AND refactor. He suggested that even when a team decides to rewrite the system they would eventually have two systems running in parallel. The old system which would require refactoring and the new system which is being rewritten. The combination becomes an overly complex task. According to him,
Maintaining an aging code base, AND writing a new system, is going to be a huge drain on your resources. Your team is split and will run into delays. You'll have to plan and carefully execute a transition. Meanwhile, your competitors don't have your time-to-market problem and will try to steal your customers. If you can stare this reality in the eye and still want to bet the firm on a rewrite, you may have a chance of succeeding.
Naresh Jain had the following suggestions specifically for legacy code. Refactor when the code is difficult to understand and the team is not sure of what it does. Rewrite when it is clear what the code does but it is difficult to understand.
Thus, refactoring is the preferred way to incrementally improve the system. It is slow paced, improves quality with small and constant improvement. Rewrite has its advantages however, in most situations it is a riskier option and the teams can never be sure about the outcome. As Joel on Software suggested,
It's important to remember that when you start from scratch there is absolutely no reason to believe that you are going to do a better job than you did the first time.
Business To know technology
by
Senthil Kumar Vaithilingam
Re: Business To know technology
by
Mark Lawler
One rewriting can be done by multiple refactoring
by
Zhang Joey
Business pressure can lead to bad code
by
Salah LAARIDH
- Misidentifying requirements / Changing of requirements frequently.
- The pressure made by the business (the owner of the application) to have something that works. In this case the first worries of the team will be delivering as soon as possible which will lead to a completely ignoring of code quality.
Re: One rewriting can be done by multiple refactoring
by
Salah LAARIDH
Good arguments against the great rewrite.
by
Alen Milkovic
Nice article, I am only missing some good arguments for the other side. Do they even exist?
Software development and entropy...
by
SSSSS RRRRR
Refactoring is incremental, therefore it is a linear process. Unfortunately, the development of software systems is far from being linear. It is actually quite non-linear and that means that some (bad) changes in the code are potentially irreversible (or would be ultra costly). The physic guys call it entropy: when the paste is out of the toothpaste tube, you will have a hard time putting it back in that same tube. In a system, the higher the entropy, the most likely you will be able to "simply" refactor.
On Joel's note "It's important to remember that when you start from scratch there is absolutely no reason to believe that you are going to do a better job than you did the first time.", two things:
1) Between developing a system with a defined set of features in 2004 and developing a system with this same set of features in 2009, there is going to be probably at least a 50% productivity advantage for the latter. Why? simply because software is one of the most efficient industries in terms of productivity gains each year (who spend time to write SQL code manually besides DBA nowadays?).
2) There is something else called the "experience curve effect". It says that the more you perform a task, the more efficient you get for that task. Even though this effect has been demonstrated for manufacturing, I also think it is very meaningful in the software development industry.
Rewrite has its place for some situations, and it is not completely stupid! :)
RE::Refactor and rewrite:
by
Shubhashish Bhowmik
Of course, if proper code reviews has been done then probably the situation called "rewrite" wont arrive and if it does then probably for advancement of certain components or new technology.
Re: One rewriting can be done by multiple refactoring
by
Roy Donasco
Re: RE::Refactor and rewrite:
by
Roy Donasco
Sometimes, refactor and rewrite has become an ordeal, even more for those who are proposing the Re factor or Rewrite suggestion. Other dev guys often think that there logic is being tormented into pieces(funny isn't it !). Even sometime you will find a file of 1000 LOC where more 500LOC are commented; reason, they can be used in latter part of the project.
Of course, if proper code reviews has been done then probably the situation called "rewrite" wont arrive and if it does then probably for advancement of certain components or new technology.
Code reviews are ideal, but let's face it, ideal situations are very rare, especially if you're tasked to improve a Java code written 10 years ago when design patterns and framework does not exist yet, and public and static modifier are abused.
Educational Content
Writing Usable APIs in Practice
Giovanni Asproni May 19, 2013




Hello stranger!
You need to Register an InfoQ account or Login to post comments. But there's so much more behind being registered.Get the most out of the InfoQ experience.
Tell us what you think