Refactor or Rewrite?

There was a recent discussion on The Code Project about the virtues of refactoring. This rather quickly turned in to a question about what the difference is between refactoring and rewriting code. I have worked for a manager who loved to say “I’m confident in my ability to refactor” whenever we would talk about making sure the initial design was solid.

So, what is the difference? I think John Simmons provided the best definition. Rewriting implies that you are throwing away all (or most) of the existing code while refactoring implies that you’re moving around the existing code and redesigning small parts of it.

I think the distinction between them is important, particularly in cases like I mentioned above. In that case, the “refactoring” that was being referred to was essentially rewriting the code each time because it wasn’t designed properly in the first place.