Monday, June 30, 2008

My favorite benefit of Unit Testing

Test Driven Development has lots of benefits: Better quality code, good code coverage, good design, and refactoring.

Refactoring is my favorite. I try to write my tests before I write my code, but sometimes, I get a little ahead of myself and jump right into code. I know what it needs to do after all, and my mind just attacks the problem and puts all the code and if statements in place to handle it. And then I realize, that I need to test my code, so I write my unit tests. As soon as the unit test starts to be cumbersome to write, I know I have a problem. I've written my code to be too complex. The Unit test should be a simple thing to set up. Now I get to refactor the code.

Test Driven development makes you think. It makes you think about what a function or object should do, and what it should not do. And I take a lot of pride in refactoring. Not just other programmers' code, but mine too. Refactoring should always improve the code. Make it more self documenting, more loosely coupled, more testable. I love taking my own code, code that would have worked, and making it better. Test Driven Development is the tool that helps me reach my goal of good code that's easy to understand.

No comments: