Refactor vs. rewrite: Deciding what to do with problem software (original) (raw)

At some point, all developers must decide whether to refactor code or rewrite it. Base this choice on factors such as architectural soundness, time, money and goals.

When application source code becomes unmanageable due to patches, bad maintenance, freewheeling feature additions or other consequences of long operation, developers must either refactor or rewrite. In a refactor, developers make microchanges to clean up the code. With a rewrite, they throw almost everything away, and the coding process essentially starts anew.

Both of these options have advantages and disadvantages. Refactoring code helps keep it manageable without major overhauls but may not set the app up for new development technologies or application languages. Rewriting code enables foundational changes to the code but risks confusing developers or even breaking the product.

Don't think in absolutes for refactoring vs. rewriting code. Instead, determine which one is better for the particular project. This choice depends on factors such as the type of application, the team's capabilities, your long- and short-term goals for the application and your appetite for risk.

Let's examine the pros and cons of both refactoring and rewrites, and guidelines to make the right determination.

Refactoring code

When a programmer refactors software, the goal is to improve the internal structure of the code without altering its external behavior. For example, developers remove redundant code or break a particularly task-heavy application component into several objects, each with a single responsibility.

The extreme programming development approach, a concept known as merciless refactoring, stresses the need to continuously refactor code. Theoretically, programmers who refactor continuously make sections of code more attractive with every change.

Refactored code should be easily understood by other people, so developers can turn code that scares people into code that people can trust and feel comfortable updating on their own.

Advantages of refactoring

Disadvantages of refactoring

The discipline of refactoring is helpful when applications rely on thousands of lines of code. Refactoring breaks that up into abstracted, high-level objects, adds testability to those objects and keeps the functions reasonably small. However, when we start talking about hundreds of thousands of lines of code, it's time to move to the next level: a code rewrite.

Rewriting code

Rather than read and analyze complex, ugly code for refactoring, programmers can opt to just write new code altogether. Unlike refactoring, code rewrites sound relatively straightforward, since the programmers just start over and replace the functionality. However, it isn't nearly that simple.

To successfully rewrite software, developers should form two teams: one that maintains the old application and another that creates the new one. This means duplicating resources. Worse, the old system, still in production, needs updates and changes to keep it working. As such, the rewrite team must constantly reconfigure plans for the future product to match those changes in the existing one.

Advantages of rewrites

Disadvantages of rewrites

Next Steps

Follow Google's lead with programming style guides

Dig Deeper on Application development and design