Press "Enter" to skip to content

Should you throw away bad code?

In an ideal world, the answer would usually be a resounding yes. In an ideal world I would also get paid more to do less work – That isn’t happening either.

It can be difficult to know when you should rewrite and when you should just leave it alone.

There are some very clearcut cases at the extremes. If it works and you don’t need to change anything, leave it alone, even if the code is terrible. On the other hand, if you are going to be making a lot of changes, it is likely to be faster to throw out the code that you will be rewriting in increments in any case.

Which brings us to the in-between cases. Basically, my measure for whether to throw out the code is to plan properly and try to decide upfront how much of the code I’m likely to rewrite in the process of making the changes. If I’m likely to end up rewriting a big percentage of the code, it is usually faster (and leads to better design), to just start with a blank screen.

In aproject I was recently involved with, a Java Enterprise system was outsourced to a developer. It turns out that he was completely incompetent. I only got involved 3 months into the project at which stage we replaced this developer and outsourced the development to a small company.

I argued that we should scrap the code and start over, but I was overruled (Because we had invested too much in the code already) and we continued with that code. The project should have taken 3 months to complete. It is now just over a year later and we are finally looking like we have solved the final performance issues. I was only involved in a desktop application that exchanges data with the online application, but I did get to see the early code for the web application, and it was not pretty.

Obviously, scrapping bad code costs money (The initial cost of development, plus the cost of redeveloping), but if the design is bad enough, not scrapping the code can cost a lot more.

On that note, I’ve just scrapped 50 000 lines of terrible code for some functionality that we want to change and extend, and the new design is so much more maintainable and flexible. If you can, throwingout really bad code can help a lot, but as with all things too much of a good thing can be really bad.