man-working-at-office-desk

I bet every developer in his lifetime has asked himself these questions multiple times whenever he has thought to refactor some sluggish code or has to make some major change to a complex piece of code-

Should I jump into this? How will I make sure it will not break an existing functionality?

This dilemma sometimes or should I say most of the times has forced us to live with that sluggish piece of code just because we were hesitant and not sure of what impact it would have or because we didn’t have much time to test the changes.

Don’t you wish for a magic wand which you can just wave to find out what all you broke or if everything is fine?

Well, there is one magic wand called Unit Tests. Yes, if we had written unit test cases with sufficient code coverage it would be as easy as waving a magic wand. All you need to do is to run them to verify the change, that’s it!

After working with startups for most part of my career, where changes are frequent and unpredictable, I have seen lot of these instances happening where adequate unit test cases were not there. People were afraid of making changes to a complex code and even if we do the changes, a lot of time was spent on testing. And if I compare it with products where we had written unit test cases, this has never been a problem. We just need to run the unit test cases to verify the change. There is an extra effort of writing them but it is like an initial investment that saves a lot of time and money going forward.

Well, there are many more advantages of writing unit test cases or doing TDD besides the one I talked about. According to me, the following are the most important reasons to write unit tests:-

  1. Helps deliver predictable and efficient systems – With higher code coverage you are sure that the code is working and are able to track inefficient code.
  2. Saves QA regression cycle – Most of the basic issues get caught while running the unit tests and that means fewer bugs are reporting in QA, thus reducing the regression cycle.
  3. Helps a great deal in planning – Unit tests can be treated as a documentation of your software and can be of great help while planning for changes.
  4. Increases engineering efficiency – With all the use cases covered in unit tests a developer can test out his/her code pretty quickly and fix bugs pretty fast.
  5. Unit tests make the code more manageable – The ability to write unit tests makes it mandatory to design a highly cohesive system. The different layers are loosely coupled so that they can be unit tested individually.