Mar 30, 2009

TDD and I

Rants

This is a re-take of one of my posts to the ALT.Net Google group. Probably a little of topic so I just want to capture it here, for future reference.

I'd like to throw in my two cents. I'm a TDD noob, so it might be relevant in this context. The point of the lengthy story that follows is that you can only lead a horse to water, as one of my colleagues likes to say when we discuss taking over the office with TDD. If one is not open to exploring new ways of doing things and discounts it right off the bat, no progress will be made. You can cite all the papers in the world, studies and research and successful projects, give examples, they will all be attributed to flukes, better funding, better building, better chairs, etc. People naturally resist change, and it will be rationalized in new and interesting ways all the time. I think the gap is not something the ALT.NET community can bridge. Each developer must try it first hand to experience the benefits. And try hard, because it’s not easy. For me the TDD showdown happened privately, I was both the TDD-er and the non-TDD-er.

That being said, big thanks go out to the folks on this board, and others, who’ve paved the way for me to become a better developer. I don’t think we’d be where we are now if it weren’t for huge contributions from guys like you. And for what it’s worth, I think the ALT.NET movement is doing a great job of moving forward the .Net world, and promoting continuous improvement. Read no further unless you have time for a rant.

I've been trying to promote TDD at work for the past few months. I've spent a lot of my personal time actually doing it to get hands on experience, scoping out different open source projects to get a feel for it (good vs bad unit tests, mocking, etc.) Here's one of the things that struck me in the learning process. I've been reading a lot - generally I agree with the literature on the benefits of TDD, but I was somewhat skeptical about the practical aspects - and accumulating a lot of theoretical knowledge. Still, when time came to write my first test I felt none the wiser. Complete writer's block – huh, where do I start kind of thing? So I've stumbled and tried and Googled and things started working together. I was pretty happy with the way things were going. Then I ran into things that needed testing that were doing a bunch of file manipulations. Uh oh, very unpleasant, no interface in sight in the System.IO namespace so no mocking. Gave up on TDD. Time passed, and I got uncomfortable with the lack of feedback. Yes, that is when I realized first hand that TDD was giving me something other than a bunch of asserts I could run and tell that everything was ok. Started TDD again, wrote wrappers for the System.IO part and wrote some integration tests on top of the unit tests to verify the files are handled correctly (I’m still not sure if this was the way to go, but it works pretty well in practice) and I was happy again.

A big moment for me was when I tried writing a validation framework. So I started with a little sketch of what I was trying to build. It had things like IRuleSet, IRule,IRuleResult and various implementations of those. Then I started coding, and wrote everything test first to see where it will lead. Well most of those things became superfluous. The problem I was trying to solve was much simpler – basically write a bunch of rules I could evaluate in a batch and tell a remote client everything that was wrong with their request in one remote call. Ultimately I just kept the IRuleSet<T> and expressed the rules as simple lambda’s that would return true or false. So I can do things like this:

   1: IRuleSet<string> rules = new RuleSet<string>();
   2: rules.Add(x => x.Length > 5, “Length must be greater than 5”)
   3:     .Add(x => x.Length < 10, “Length must be less than 10”)
   4:     .Add(x => x.Contains("TDD"), "Must contain TDD")
   5: List<string> brokenRules = rules.RulesBrokenBy (“TDD Rocks!”);

I liked this better than the objects soup I initially cooked up, and it works very well with some caveats. I’m sure that this would have taken a lot longer to achieve without the TDD approach, as it made me realize what my real requirements were, and that I didn’t need all that complexity I was imagining in the beginning.

Lots more tests have been written since and lots more code. The overall quality has improved and getting better every day. It is however a process of continuous improvement and it takes work. It would be hard now to turn back and do thing the old fashion way. The one thing I could have used is a good mentor to provide some guidance. Failing that, I turned to ALT.NET and the huge contributions made by the members, and it’s helped in more ways than I can describe. Thanks again to everyone that uses their free time to contribute and guide other devs! Kudos! Sorry for the rant, and slightly off topic post.