I came accross a pretty good post on refactoring today on Basildon Coder. The post offers some sound advice on when and how to refactor code. I found the following paragraph quite insightful regarding completely rewriting existing code.
"The problem is that warty old code isn’t always just warty - it’s battle-scarred. It has years of tweaks and bug-fixes in there to deal with all sorts of edge conditions and obscure environments. Throw that out and replace it with pristine new code, and you’ll often find that a load of very old issues suddenly come back to haunt you."
All too often we come accross code we don't like. I've come across some real gems in my career, and I am positive that I have created some truly horrible code in my life. I think that the first reaction that we have when we see ugly code or poorly designed systems is to push for a complete rewrite. This is a normal reaction, but I think that a complete re-write should really only be used as a last resort. It will always be a bigger task than it initially seems. The best choice, and often more challenging is to figure out a way to refactor the problem code.