Test-Driven Development (TDD) - Good or Bad? I don't know.

by Erik Lane 3. April 2006 06:17

Since I got my iPod I've subscribed to the DotNetRocks (DNR) podcast.  The first one I listened to was from 3/21 with Jean-Paul Boodhoo on TDD.  It was enlightening to say the least.  Someone who uses it daily, designs software with it, and can give real life experience on the topic was awesome! 

We don't use TDD where I work but I've used it on a few smaller projects and personal projects so I could see what the fuss was all about.  After listening to Jean-Paul I realized I really wasn't doing TDD all.  I would write some code and then right the unit test for the code.  According the Jean-Paul, TDD is where you write the unit test against your idea of what the code will do and then write the code to make the test pass.  Think about that.  You are writing a test for code that doesn't even exist.  So basically you have an idea of what you want your code to do, what results that should return from it, and that's what you use to write your tests.  That's a mind shift and something to get your head around.  Another thing to get your head around is that TDD is not a testing strategy but a design strategy.  I think I just got this listening to Jean-Paul's interview.  One thing I've felt as an advantage to TDD is the extra thought that has to go into your design and how the pieces will work together.  I don't know about you but more than once I've sat down to write some code and then coded myself into a corner and had to do some heavy refactoring to clean it up.  Maybe, don't know for sure, but maybe if I was doing it TDD style I would've seen something like that coming sooner?  Or maybe it is just me.  :-)

What prompted me to even blog about this was Jeffery Palermo's post last Friday but more importantly the comments.  In the comments are somewhat of a debate on what is good/bad/ugly with TDD.  The comment that stuck out the most was Brian Sherwin's:

 Brian Sherwin said:
 The biggest problem here is that there are really three groups that don't get along real well with each other.
 First, there is the group that says "ALL code has to have a test FIRST" (hardcore TDD).
 The second group says, "We don't have time to write all of these tests before we write code--too much time is built running the tests just to see red when I know it's going to fail.  That's just STUPID." (TDD is for idiots).
 The third group says, "Wow, unit testing gives me confidence to refactor.  I should have unit tests to cover all my code.  I should at least be thinking of how to write the test as I'm doing the development, and ideally, I write that test soon after I wrote the code.  TDD is too much, not TDD is too little."  (Both sides can work.)
 I'll be honest--I don't TDD *ALL* of my code.  I do however TDD any refactorings.  Recently I was rewriting a module of a huge application I am working with, and the approach was--write a test that exercised the method I was replacing/rewrite code/verify new method returns the exact same results.
     Assert.AreEqual(Obj1.TestMethod, Obj2.TestMethod)

I totally agree with him.  I typically fall into group #2 but I'm think I'm slowly moving to group #3.  Group #1 is too absolute for me and to me there is only one thing that is absolute.

Anywho, there are also two DNRTv episodes (#1 & #2) where Jean-Paul develops an app from scratch using TDD.  I've got them downloaded and ready to go.  I'm sure they are great as Jean-Paul is on the teaching side of TDD and not the "if you don't use TDD you stink" side. 

Giddy Up!

Tags:

Comments

Joel Ross
Joel Ross on 4/1/2006 4:38:00 PM

I watched the two episodes with Jean-Paul, and while I watched them, I thought it was very cool to see how you can come up with a solid design - he demonstrates how to do Dependancy Injection, a good use of interfaces, etc.

But when it was all done, he took two hours to bind a collection to a drop down. When I stepped back and looked at what was actually performed, I was a little bit disappointed.

eriklane
eriklane United States on 4/1/2006 4:48:00 PM

Hey Joel.  Yeah that's also been an experience of mine.  However, that's the initial hurdle of getting it up and going I think.  From that point going forward he has a benchmark for what is acceptable...no matter what changes or how complex it gets under the hood.  

Later down the road after enhancements etc.. that benchmark would still be valid and less time on debugging especially if its a new/different developer doing the work.

I think that's why I'm in group #2 still but leaning toward group #3.  I can see both sides.  Thanks for the feedback!

peter
peter United Kingdom on 11/9/2008 3:51:08 AM

Hey !
Just came across you post. So have you decided which camp you're  in (2 or 3) and why??

eriklane
eriklane United States on 11/9/2008 4:18:54 PM

Peter - After re-reading this post and practicing TDD for the last two years, I am card caring member of camp 3 and a strong believer in camp 1.  Camp 1 is still too absolute but I must say that my first thought is to write a test before I write any code (camp 1) but, at a minimum, I write a tests to support any code changes/refactoring.  I have found that any tests that I have makes life as a developer much easier.

peter
peter on 11/10/2008 6:26:35 AM

Hi!
Can you site some specific examples why YOU think its made an improvement?

BTW. what toolset do u you, do u you use Rhino or nMock?

Thanx,

eriklane
eriklane United States on 11/11/2008 6:36:28 AM

Peter - Here are a couple of specific examples:

1.  I write code in smaller pieces.  I focus on one piece of functionality at a time vs. the whole application.

2.  Following along with #1, the smaller pieces are written from the perspective that something else with be interacting with it but I'm not really sure yet (since it hasn't been written or tested).  So I look at code from an API perspective so it can support my tests as well as live code.

3.  When I have tests that pass, based on a small piece of functionality, then when I need to refactor I have confidence when the tests continue to pass.

Tool set:  I use NUnit and ReSharper and I don't use mocks as much as I should so I can't really comment on one.

Comments are closed