Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 500
John Channing Avatar asked Aug 29 '08 20:08

John Channing


People also ask

When should you rewrite code?

A piece of software is typically rewritten when one or more of the following apply: its source code is not available or is only available under an incompatible license. its code cannot be adapted to a new target platform. its existing code has become too difficult to handle and extend.

What is the difference between refactoring and rewriting?

In a refactor, developers make microchanges to clean up the code. With a rewrite, they throw almost everything away, and the coding process essentially starts anew.

What do you call rewriting code?

In computer programming and software design, code refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior.


2 Answers

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.

like image 116
JosephStyons Avatar answered Sep 19 '22 05:09

JosephStyons


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.

like image 42
nsanders Avatar answered Sep 21 '22 05:09

nsanders