Sunday, March 12, 2017

Ground Rules for Refactoring Legacy Systems

If you have a very poor code which is buggy, cluttered, ugly, scary, etc. If you have such code,  do one of the following:
  1. Sunset this application
  2. Ask your customers to stop asking for changes and fixes
  3. Quit your job and switch to a more convenient one (other than software of course)
If you can't do any of these, then you're stuck with refactoring this code base. This is possible and even exciting.

Here is a set of ground rules to take into consideration before you start doing that:
  1. Zero "more" defects policy: That is, stop working the moment you find the next bug, and fix it immediately. The rule is adapted from the Zero defects policy! which is a bit overwhelming at this stage.
  2. Any "new" code must be covered by tests. Period.
  3. Peer review is part of our DNA (till we're able to automate parts of this review, or all of it)
  4. The Boy-Scout Rule applies to all of us. 
  5. Automate all donkey work, one script at a time! That is, everything we do in a systematic way, can be scripted. What we should do is to automate all this work. 
If you do that, you'll find things slightly improving at the beginning, but exponentially improving later on, insha'Allah :)

Friday, March 3, 2017

How to find dead code?

Recently, I have read this interview with Kevlin Henney, an independent consultant and a great developer.

These are some very good insights about ways to find dead code:
  1. Static analyzers are a good start
  2. Search for files that has never changed since a while. "There are many reasons code may be stable — it’s just right, it’s just dead, it’s just too scary — but unless you investigate you’ll never know."
  3. Runtime monitoring and Dynamic program analysis may be used to rule out parts of the code which is not dead code, effectively reducing the code under investigation
One very interesting note is that:
"Deleting dead code is not a technical problem; it is a problem of mindset and culture."
Furthermore, in this very interesting lecture by Kevlin, he tells the very famous story about Kinght corporation which lost hundreds of millions due some dead code left over. Also, narrates some very interesting stories alike. I highly recommend any developer to watch this lecture.