“Twelve Benefits of Writing Unit Tests First”

I ran across this post today. It’s more than a year old, but it’s still quite valid.

I discovered the joys of unit tests a couple years ago, when I was developing an internal library of C++ classes and functions. Every time I made a change, I would run the test code for the library feature(s) that I was working on. It caught a lot of bugs before they made it into the programs that were actually using the library, but it had a totally unexpected side effect: I felt a lot freer to experiment with the code, knowing that any change that broke something would probably be caught immediately. It also gave me a lot more confidence in my code, without exhaustive manual testing all the time. It seems like more work, but in the end it will save you a lot more time and effort than it costs.

I haven’t tried writing the unit tests before writing the actual code yet, but after reading that post, I can see several of the advantages to it. I’ll try that next time.