Last time, I spoke of the benefits of Test Driven Development, but there’s only so much I could cover in a small blog post. Today, I will cover two benefits of TDD: confidence and documentation.
Whenever you make a change to code without tests, you risk breaking some functionality unless you test the code manually. Even when you test the code manually, the individual cases that you tested may work, but there are edge cases you do not test or do not think of that will fail. As code gets larger and more complicated, it gets harder to make changes without breaking things because the code becomes more complex and interrelated. It is true that automated testing and writing tests after the implementation can solve much of that problem. But writing tests after the fact means that your tests can be biased towards what your code will do, and not necessarily what you want your code to do. And again, writing your tests after the fact may not catch all the scenarios that your code will be put through. In TDD, you try to think of all your cases and scenarios first, and then you start implementing solutions for each case one at a time. You may add or remove cases as you discover more information while working. But when you are done, you can say with confidence that this code will do everything that I am testing for, and if any changes are made to the code, the tests will tell me if something breaks.
Tests tell me what the code can do. It serves as my documentation for the code’s functionality. Developers can look at my tests and see the method calls and the setup needed to utilize my code. If the code was test driven, then the tests should comprehensively tell me what my code can do. I should not have excess code or obsolete code that is undocumented by tests. I am confident that the code will perform whatever is documented in my tests, and no less than that.
I have spoken about the benefits of Test Driven Development for a while. I’m sure there are people who have questions or comments about the downsides of TDD. I personally believe that any downsides are negligible, or countered by another benefit. That will be the topic for another post.