views:

471

answers:

11
+8  Q: 

Rewrite or repair?

I'm sure you have all been there, you take on a project where there is a creaky old code base which is barely fit for purpose and you have to make the decision to either re-write it from scratch or repair what already exists.

Conventional wisdom tends to suggest that you should never attempt a re-write from scratch as the risk of failure is very high. So what did you do when faced with this problem, how did you make the decision and how did it turn out?

A: 

It's not so black and white... it really depends on a lot of factors (the more important being "what does the person paying you want you to do")

Where I work we re-wrote a development framework, and on the other hand, we keep modifying some old systems that cannot be migrated (because of the client's technology and time restrictions). In this case, we try to mantain the coding style and sometimes you have to implement a lot of workarounds because of the way it was built

Juan Manuel
+1  A: 

Refactor unless it is very bad indeed.

Joel has a lot to say on this...

At the very least, rewrite the code with the old code in front of you and don't just start over from scratch. The old code may be terrible, but it is the way it is for a reason and if you ignore it you'll end up seeing the same bugs that were probably fixed years ago in the old code.

levand
Where is the dividing line between just bad and very bad?
Scott Whitlock
+3  A: 

See Joel Spolsky's essay Things You Should Never Do. In summary, when you rewrite you lose all the lessons you learned to make your current code work the way it needs to work.

See also: Big Ball of Mud

Sam Hasler
+8  A: 

Just clean up the code a little bit every time you work with it. If there isn't one already, setup a unit testing framework. All new code should get tests written. Any old code you fix as a result of bugs, try to slide in tests too.

As the cleanups progress, you'll be able to sweep more and more of the nasty code into encapsulated bins. Then you can pick those off one by one in the future.

A tool like javadoc or doxygen, if not already in use, can also help improve code documentation and comprehensibility.

The arguments against a complete rewrite a pretty strong. Those tons of "little bugs" and behaviors that were coded in over the time frame of the original project will sneak right back in again.

nsanders
+2  A: 

One reason for rewriting at one of my previous jobs was an inability to find developers with enough experience to work on the original code base.

The decision was made to first clean up the underlying database structure, then rewrite in something that would make it easier to find full-time employees and/or contractors.

I haven't heard yet how it worked out :)

I think people have a tendency to go for rewrites because it seems more fun on the surface.

We get to rebuild from scratch!

We'll do it right this time!

etc.

Mark Biek
+2  A: 

It is rare for a re-write of anything complex to succeed. It's tempting, but a low percentage strategy.

Get legacy code under unit tests and refactor it, and/or completely replace small portions of it incrementally when opportune.

Dave Ward
+5  A: 

It really depends on how bad it is.

If it's a small system, and you fully understand it, then a rewrite is not crazy.

On the other hand, if it's a giant legacy monster with ten million lines of undocumented mystery code, then you're really going to have a hard time with a full rewrite.

Points to consider:

  • If it looks good to the user, they won't care what kind of spaghetti mess it is for you. On the other hand, if it's bad for them too, then it's easier to get agreement (and patience).
  • If you do rewrite, try to do it one part at a time. A messy, disorganized codebase may make this difficult (i.e, replacing just one part requires a rewrite of large icebergs of dependency code), but if possible, this makes it a lot easier to gradually do the rewrite and get feedback from users along the way.

I would really hesitate to take on a giant rewrite project for a large system without being able to release the new edition one part at a time.

JosephStyons
+2  A: 

There is a new book coming out, Brownfield Application Development in .NET by Baley and Belcham. The first chapter is free, and talks about these issues from a mostly platform agnostic perspective.

Forgotten Semicolon
+1  A: 

Depending on your situation, you might have another option: in-license third-party code.

I've consulted at a couple of companies where that would be the sensible choice, although seemingly "throwing away IP" can be a big barrier for management. At my current company, we seriously considered the viable option of using third-party code to replace our core framework, but that idea was ultimately rejected more for business reasons than technical reasons.

To directly answer your question, we finally chose to rewrite the legacy framework - a decision we didn't take lightly! 14 months on, we don't regret this choice at all. Just considering the time spent fixing bugs, our new framework has nearly paid for itself. On the negative side, it is not quite feature-complete yet so we are in the unenviable position of maintaining two separate frameworks in parallel until we can port the last of our "front-end" applications.

Stewart
A: 

I highly recommend reading "Working Effectively with Legacy Code" by Michael Feathers. It's coaching advice on how to refactor your code so that it is unit testable.

Matt Cruikshank
+1  A: 

Repair, or more importantly, refactor. Both because Joel said so and also because, if it's your code, you've probably learned a ton more stuff since you touched this code last. If you wrote it in .NET 1.1, you can upgrade it to 3.5 SP1. You get to go in and purge all the old commented out code. You're 100x better as a developer now than when you first wrote this code.

The one exception I think is when the code uses really antiquated technologies - in which case you might be better served by writing a new version. If you're looking at some VB6 app with 10,000 lines of code with an Access database backend obviously set up by someone who didn't know much about how databases work (which could very well be you eight years ago) then you can probably pull off a quicker, C#/SQL-based solution in a fraction of the time and code.

Schnapple