Friday, July 29, 2016

Dealing with Legacy Code.



Our life as developers' would be easy if we write only new code.No bug fixing or editing yesterday code It’d be pretty nice, right?

Definition of Legacy code


 Legacy code
Legacy code was defined in many ways and there is no common definition to it even in wikipedia, may by.
  • Code that’s part of an older version of the software.
  • Code that relates to a no-longer supported hardware or software dependency.
  • Code inherited from someone else.
  • Code that isn’t covered by automated unit tests.
To me, legacy code is simply code without tests. ~ Michael Feathers
Well, that is the first formal definition of the expression legacy code, published by Michael Feathers in his book Working Effectively with Legacy Code . it seems that Legacy code is "Difficult to change" but term "Difficult to change" is so vague and we need solution.
But guess it mostly like this picture when re-factoring legacy code.
Refactoring legacy code
Refactoring legacy code


Defensive strategies.

1- Be conventional.

Following standards and language convention make code easy to work with.

2- Code review.

We took about it in previous post but talk to your peer about code you wrote make it more readable.   


Working strategies.

1- Don't make it personal.

We always think it’s faster to rewrite it from scratch than try to understand what other developers did.Don't judge other developer and speak bad about code even it worst code you ever seen.

2- Testing.

Automated unit tests will give developer understanding about key things: dependencies this piece of code has, input data, output results, boundary conditions so on.From here you can walk in the minefield.

3- Start small.

It always seems like a disaster at first, Fear is your worst enemy in facing legacy code.You must start from somewhere.Small bug fixes or even change variable names that make code more meaningful that make working with old code infinitely easier.

4- Make your checklist.

Checklists it underestimated but guess what it works all the time make your own checklist,"Identify changes, find test points, break dependencies, write test, make change and refactor.

Image sources