Thursday, May 05, 2005

Refactoring is optimization

Refactoring is a form of Optimization. You are optimizing the code for maintainability and clarity.

The rule of premature optimization is that you don't do it till you need it. This prevents you spending time optimizing code that doesn't need it. You measure where you need to optimize before optimizing. In reality the optimizations needed are never where you would have guessed. So the question is... In the case of refactoring, what is premature optimization?

In my opinion, premature optimization is when you design the code before you write it. You are guessing at what concepts you are going to need to hold in the code, and not being driven by the actual need of the system. You end up over-designing system that a simple solution would work just a well for. (see YAGNI).

Hence the order of Red, Green, Refactor.
* Red - Add an idea to the requirements.
* Green - Add the concept to the code.
* Refactor - Distill the new concepts for clarity.

Note that refactor is after the idea is in the system, not before!

These thoughts came out of a chat with Simon Harris the other week.

Post a Comment

GitHub Projects