Monday, February 23, 2009

Starting From Scratch? Never Say Never

While following my blog quest, I ran across an interesting article which told several compelling reasons why the PHP group is not just re-writing the PHP code from scratch for the upcoming PHP 6.

As I was sifting (or slogging, perhaps) through the comments, I found a link to an interesting article entitled, Things You Should Never Do, Part I by Joel. My curiosity sufficiently piqued, I started reading.

At the risk of over-simplfying, the author basically says, "Never rewrite your code from scratch." It's a bold statement. I can certainly appreciate the intention behind it. From my own experience, I know that rewriting code from scratch can be a long and daunting process.

Most programmers I know (with myself as a definite inclusion), tend to look back at their "old" code and think, "I could do that better if I rewrote it today." Sometimes the code really isn't very old at all.. only a few months. For the most part, I think that having that feeling is a good thing. It means that you've been learning. It means that you have the drive to improve yourself and your work. But acting on that feeling often leads to trouble, just as Joel says. I like how he points out that new code isn't necessarily better than old code -- old code does not rust.

I also concur with his assessment that so-called "old" code is better tested. It is more mature. And I see where I sometimes need to repent of saying that the legacy code is a mess. Joel hits the nail on the head when he identifies that old code looks messy because it is easier to write code than to read it.

So I agree with the spirit of the article. But I take umbrage with a particular word: never.

You (or a software company) should never rewrite their code from scratch? That I don't believe. Now I realize that Joel's article was written back in 2000. I haven't followed his blog to find out if there was a Part II, or if he recanted what he said. But I believe there are some instances where it can be a very good thing to rewrite your software from scratch.

After pondering for a while, I came up with the following list of times when rewriting your code may be beneficial:
  • When you are building something new. Not just a new version, mind you. A new product or perhaps a paradigm shift on an old product.
  • When you are adding new features which the old code cannot support without difficulty. Sometimes you can be painted into a corner by legacy code.
  • When you want to take advantage of new technologies or languages.

I find that Mac OS X is a great example of how rewriting code can be a good thing. Rather than take OS 9 (which was a mature piece of software with a solid background) and turn out a new-and-improved version, they scrapped it instead. They rewrote things from the ground up. They ended up with a terrific operating system... my favorite, in fact.

[I'm adding this aside to recognize that OS X was built on some "old" code, specifically UNIX and NeXT. Although it was not written completely from scratch, I think it still exemplifies the point I am trying to make.]

Now there was a painful transition from OS 9 to OS X. For a while the best programs were not available for OS X. Users clung on to the last vestiges of OS 9. But over time, new software emerged. I think many Mac owners are now glad that they don't have to worry about "Classic" mode anymore.

Personally, I think that this is something that Windows could benefit from. They've taken many pages from Steve Jobs' book, why not this one too? A lot of the issues (especially security issues) in Windows linger because of backward compatibility. But a new OS would mean that old programs wouldn't work anymore. Microsoft, Windows programmers, and users would all have to take a leap of faith. But it can be done.

Another example is with some software my previous company was developing. We had some legacy AS2 / C code which was really just a nightmare to maintain, let alone improve. We looked at the situation and decided that we should take advantage of the new AS3 and rewrite. The new product turned out much better than the old one, both for the user and programmer. That's not to say it was perfect, but a definite improvement.

There are probably some other times when a total code rewrite would be beneficial. So certainly we shouldn't say never. But that decision needs to be made cautiously, with eyes open to the risks. If possible, keep up development on the previous version of the software.

I suppose that the moral of the story is that there is a time and season for all things -- even rewriting code from scratch.