Thursday, February 3, 2005

TDD - First Impressions

      I spent the last two days in a class covering test driven development, unit testing, and refactoring.  I hope to provide a more detailed discussion of what I learned at some later point but for now I thought I'd post my initial impressions.   I've read a lot about TDD, Unit Testing, Refactoring, etc. but I'd never actually *done* test driven development.  The class had several hands-on exercises.  We were using C# and NUnit.  Let me say right here that NUnit is a slick app.  It is unobtrusive which is exactly what you want in a test harness.  This is the report of that experience.

      First off, it felt really strange.  Generally when developing a program, you think about the big picture first and work your way to the details.  With TDD, you end up taking the opposite tack.  Because you have to write a test, fail the test, then write the code, you cannot start with the big parts, instead you start with the small internals.  Of course it is necessary to put some thought into the bigger picture, but very quickly you are driven into the implementation details and that creates a feedback loop for your big picture design.  This feels strange but you become accustomed to it.  The designs that come out seem clean.  Forcing testability causes you to think about cohesion, coupling, redundancy, etc.

      When you are doing test driven development, you are constantly switching between the editor, compiler, and test harness.  You are compiling often.  This is a good thing.  It means that your code is always working.  You don't go on long coding sprees followed by long bug fixing sprees.  Instead, you intermingle the two a lot.  I find it easier to fix an issue as I'm writing it rather than later when I compile some larger chunk of code.

      TDD feels good.  Every time you run the unit tests, you get positive feedback that things are working.  When you make a change, even a radical change, you know that if all the test cases pass that everything is working.  It gives you peace of mind that you cannot get in older coding models.  If you've never done any test driven development, give it a try.  Who knows, you might like it.

      The class was taught by Net Objectives.  This is the 3rd class I've taken from them.  I've also attended a few of their free seminars.  If you have an interest in OO or Agile techniques, check them out.  I highly recommend their work.

6 comments:

  1. I posted about my impressions about TDD when you're tied to a third party library. I had the same impressions and feedback that you posted about and I'm really into TDD as I find it a really smart methodology when developing in small teams or as a one-person team.


    Maybe you're interested: http://rgabostyle.com/blog/archive/2005/02/04/157.aspx


    Welcome to TDD world ;)

    ReplyDelete
  2. +1


    I love TDD. It's nothing new though. For me, it is putting the granular testing of code in the hands of the person who knows it best - the developer. And the byproduct is that you know your intentions before coding - that means increased focus.


    Simply Great!


    Oh, one more thing, we have an XML schema for generating documentation from comments. How about a similar schema to generate NUnit tests? I mean, you write your tests above your method definition in XML. What do you think?

    ReplyDelete
  3. I dig it. It really has changed my perspective on development, and I'm certainly writing better code because of it.

    ReplyDelete
  4. The idea for an XML schema for generating tests is interesting. Would you describe the test in XML so it is generated for you or just have the XML wrap C# (or whatever language) code used to test the object?

    If it is the former, wouldn't it be really hard to describe anything but a basic test? Creating an object, calling a method with a parameter, calling another method, and comparing the results would be easy. Verifying something more complex would require quite complex code.

    If it is the latter, wouldn't attributes or even preprocessor commands suffice?


    Steve

    ReplyDelete
  5. "If it is the former, wouldn't it be really hard to describe anything but a basic test? Creating an object, calling a method with a parameter, calling another method, and comparing the results would be easy. Verifying something more complex would require quite complex code."

    - I agree. A very valid observation indeed. But the thought still lingers on. I will think more on it, and try to devise a better one.

    Thanks a lot for your reply!

    ReplyDelete
  6. Some more references for TDD with NUnit:

    http://www.testdriven.com/search.php?query=nunit&mid=7&action=showall&andor=AND


    About the XML schema, I think the idea to generate NUnit tests from XML has been floating around, possibly from Roy Osherove or Jonathan de Halleux.

    ReplyDelete