Thursday, July 5, 2012

Should we unit test legacy code?

This is a more frequent question I hear from my colleagues. Well my answer yes provided that the code will not thrown out tomorrow and the cost of writing will not eat up your budget.

Well there are some challenges to this especially with legacy code.  Well you need to refactore your code so it's mockable in areas you need to mock. I know there are people who says "Don't touch that code if its working." Well aren't you already touching it. What harm it will do if I do bit more changes and refactor the code so that's testable and I can test it and make sure I don't break any thing. Do we need to live with that stinking code forever. The more you modify the more it will stink if you don't clean it today.

Well personally I don't agree with it. If the cost you put today will have good ROI then we should do it. This will secure your legacy code from future modification too.

Well we should consider unit testing as tool that you will use to verify your code you just wrote. if you are following TDD this really easy.

So I wish some day the people who say no to unit testing or people who thinks its a wast of time will find its advantages and power :)