From CodeGen to TDD

One of my readers asked me a question that I thought I would share the answer to publicly. I have omitted his name to maintain his privacy:


I have been reading your blog for quite sometime now and had a quick question on TDD. I saw your screencasts and saw that you develop your tests first and then your actual classes. I use CodeSmith to generate most of my code although I tweak it to not be modeled exactly around my tables. Given this how do I make the jump to TDD? Using the method you showed, wouldn’t it take a very long time to finish the project as you are hand coding each class one by one. I basically hand coded one module (from ASPX to all the way upto the backend) and then designed my CodeSmith templates around it.

My Answer:

The first step for you to make the jump to TDD properly is to drop the code gen tools for a while. The whole premise of TDD is to design your code using tests as the design artifact to help drive out the solution incrementally. Most code gen tools do a big bang approach of code gen which you as the developer then need to go in an tweak to make work for your scenario. Utilizing code gen will not help you get into the habit of:<?xml:namespace prefix =“” o ns =“” “urn:schemas-microsoft-com:office:office” /><o:p></o:p>

<o:p> </o:p>

· Writing a failing test for a requirement. <o:p></o:p>

· Getting the test to compile<o:p></o:p>

· Coding up the necessary behavior to make the test pass.<o:p></o:p>

· Cleaning up<o:p></o:p>

· Continuing<o:p></o:p>

<o:p> </o:p>

One of the main reasons people have a hard time getting into TDD is that it is a radical departure from the way most people normally develop. In the beginning you will walk slowly, stumbling and falling often. The tests that you initially write may not be overly good. This is because you are now crafting your skills in a new art. Over time, what initially seemed alien and uncomfortable will seem normal, rapid, and welcoming.<o:p></o:p>

<o:p> </o:p>

‘Wouldn’t it take a very long time to finish the project as you are hand coding each class one by one?’<o:p></o:p>

<o:p> </o:p>

One of the things that is hard to appreciate from the small video that I showed on DNRTV, is the tools and techniques that come into play when you become a proficient test driven developer. In the class that I just finished teaching in Richmond,VA, we built a full portion of an enterprise e-commerce application with a Rich Domain Model, O/R Mapping Layer etc all over the course of a week using Test Driven Development, Design Patterns etc. One of the comments that someone made was the fact that not once during the course of the week did I use studio to either compile or run the project!<o:p></o:p>

<o:p> </o:p>

Again, that might seem like something out of the blue, but it is the little things that make you much more proficient as you get more accustomed to TDD. Once you are into the swing of it, you can bring your CodeGen tools back into the mix where they make sense. More often than not, most people who get swallowed up by TDD start to seriously question the value of Code Gen tools. It’s not to say that they don’t have their place, their use just becomes considerably diminished to how you may be using it right now.<o:p></o:p>

<o:p> </o:p>

I hope this answers your question.<o:p></o:p>