To Rewrite or Refactor – That is the Question!

Martin Fowler heavily promoted and encourage the engineering concept of Refactoring. In “Refactoring: Improving the Design of Existing Code,” Fowler discusses the basics behind how refactoring can make code simpler and easier to maintain. In addition to refactoring, the book identifies several other factor for improving code – all good techniques that software engineers should have on their “tool belt.”

However, while Refactoring is a great reference, two important areas are overlooked – making the case for the differences between refactoring and rewriting, and when should a modernization activity use either approach? To address both points, here are a few key qualitative and quantitative items to consider:

Refactoring

- Improving the design of existing code

- Make changes to the code without changing its external behavioral

- Should be part of an ongoing engineering process

Consideration:

- For a given architectural change, less than 20% of the code is rewritten

- For a given set of code, less than 20% of the architecture change

- Costs of writing new code/modifying existing code > cost of understanding the code

- For some methods with cyclomatic complexity < 20
Rewriting

- Improving the code of an existing design

- Can introduce new behaviors

- Should be considered a one time/limited time activity

Consideration:

- For a given set of code, more than 20% of the architecture changes

- For a given architecture, more than 20% of the code needs to be changed

- Costs of understanding the code > 10x cost of writing/modifying code

- For all methods with cyclomatic complexity > 50

~ by Dr. J on September 22, 2008.

Leave a Reply