blogger counters

Wednesday, September 13, 2006

How I Learned to Stop Worrying and Love TDD

I've been a developer for many years, and I've prided myself in writing very reliable code. For example, The Norton Commander 3.0, the last version I wrote, survived for two years without a single maintenance release. One of the key factors that kept me writing reliable code was fear. The fear that I would break the code and create a problem for users. I was always afraid that any major changes I made would break things. So whenever I made a major change, and I didn't let fear keep me from refactoring, I took my time and did lots of testing along the way. Fear was always at the back of my mind while I made these changes, so I really stressed about making sure I didn't make any mistakes.

That was before I discovered Test-Driven Development (TDD). I was introduced to TDD while contracting at Microsoft's patterns & practices group, working on the Mobile Client Software Factory. The more I've used TDD, the more I like it.

I've now spent about a month working on a new project at p&p (more on this soon) and I had an epiphany today. I just realized that even though that fear is still there, it's much less grounded. Yesterday and today I did two major refactorings that before would have required very careful work and testing. However this time it was nearly painless. The unit tests I've written now do that work for me. So when I did the refactoring yesterday, roughly 10% of the unit tests failed. It only took me about 15 minutes to fix the code so all the tests passed. Total time to make the refactoring was a lot less than it would have been using my old, fear-based approach to writing reliable code.

And this morning, to top it off, I just made another major refactoring change. Much to my amazement, I didn't break any of the unit tests. So after about 15 minutes of refactoring, I was done! Long live TDD!