Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrain one's self from the overwhelming urge to rewrite everything?

I'm a big fan of making lists!

As soon as the urge takes you to re-write something - spend 10 minutes making a list of the things that need re-writing. Follow all of the alleys that take you further into the code that needs attention and list those, too.

Hopefully within a relatively short space of time you'll have one of two things:

  • A really long list that completely puts you off wanting to rewrite anything ever again.
  • A list which actually isn't that long, so why not indulge yourself and go for a re-write?!

I've found that spending two months fixing bugs caused by my "harmless" re-write was enough to cure me of the urge to do these sorts of things without a clear mandate/project plan to do so.


The most memorable project of this kind for me occured some 4 years ago when I was called in to a remote office to "help" with a project that was due in 1 week for major presentation to the client and was not working yet at all. The project had been primarily off-shored to India, and IMO, a project management failure resulted in a ton of spaghetti code that was too fragmented to ever work properly in its current form.

After a full day's review, I presented my opinion to the management that the project simply needed wholesale refactoring and reorganization or it would never work properly. The result of this discussion was 6 days of 20 hours work / 4 hours sleep, 2 of which I actually spent sleeping on the couch in the company lobby due to the wasted time when driving back to the hotel.

The major improvements to the code included:

  • Application of naming standards
  • Moved into source control
  • Development of a build process
  • Documentation of the individual components

Most of the original code was left in place, but simply moved and reorganized / refactored to make it sustainable in the long term. Was it hell week? Sure. Did it make the project more successful? Yep.

I can't live with spaghetti code, and I'll often donate my own personal time to address it.


How to restrain one’s self from the overwhelming urge to rewrite everything?

Become old*

As this has happened to me, I've gradually lost the urge to rewrite everything. Why?

  • Once you've done it a few times, you realise that you often end up worse off than you started.
  • Even if you're god's gift to programming and your brilliant rewrite introduces no new bugs, you'll simply fail to notice or implement about 30% of the small edge-case features/bugs which things rely on. This will cost you months in fixing
  • Nothing wears down your irrational exuberance like time. Often this is a sad loss, but in this case, it's a win.

*(more experience may also be a suitable substitute if you lack the free time to become old)


The impulse to rewrite is righteous, provided that:

  • you "freeze" the existing code (with a label)
  • you start the rewrite attempt in a separate branch
  • you prepare first some unit-test in order to ascertain the current behavior and make sure you reproduce the existing features...

That said, you have to balance the rewriting process with the measure stability of the legacy code.
"If it is not broken, do not fix it" ;)