Aussie AI
Refactoring versus Rewriting
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Refactoring versus Rewriting
Refactoring was something I was doing for years, but I called it “code cleanup.” The seminal work on refactoring is Martin Fowler's book “Refactoring” from 1999. This was the first work to gain traction in popularizing and formalizing the ideas of cleaning up code into a disciplined approach.
Refactoring is a code maintenance task that you do mainly for code quality reasons, and it needs to be considered an overhead cost. True refactoring does not add any new functionality for customers, and marketing won't be happy if you do refactoring all day long. But refactoring is a powerful way to achieve consistency in code quality and adhere to principles such as DRY. In highly technical special cases such as writing an API, you'll need to refactor multiple times until the API is “good.”
Rewriting is where you pick up the dusty server containing the old source code repo, walk over to the office window and toss it out. You watch it smash ten floors below, drive over to CompUSA to buy a new server, and then start tapping away with a big smile on your face.
The goals of refactoring and rewriting are quite different. Refactoring aims to:
- Make the existing code “better” (e.g. modularized, layered).
- Add unit testing and other formality.
- Retain all the old features and functionality.
- Not add any new functionality.
Rewriting projects tend to:
- Throw away all the existing code.
- Choose a new tech stack, new UI, new tools, etc.
- Not support backward compatibility.
- Add some new functionality.
Refactoring and rewriting are very close together, and there's a lot of middle ground between them. If you're fixing some old code by rewriting one of the main modules, is it refactoring or rewriting?
The reality is that rewriting versus refactoring is always an engineering choice, and it's a difficult one without a clear right or wrong answer. You can't try both to see which one works better, so there's never any proof either way.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |