Home > TDD > JAOO 2007 – TDD tutorial with Robert C. Martin

JAOO 2007 – TDD tutorial with Robert C. Martin

September 30th, 2007 Leave a comment Go to comments

I arrived yesterday to Aarhus in Denmark for the ongoing JAOO conference. I’m here for two tutorials, one about Test-driven development (TDD) which I went to today, and one about domain-driven development (DDD) which I will visit tomorrow.

The TDD tutuorial was held by Robert C. Martin, also called Uncle Bob, whom have written a couple of books (none of which I have read yet, unfortunately). I found Robert to be charismatic, loud-mouthed (not in a negative way) and generally a funny guy.

The tutorial begun with a short introduction of TDD and then went on with a presentation of the requirements to a game called “Hunt the Wumpus” (wtf is a Wumpus?!). The requirements were written in a tool called FitNesse. FitNesse is a popular tool for writing what is called acceptance tests. These acceptance tests should non-programmers, typically QA or management people, be able to write.

After the introduction we were given an incomplete implementation of the game, along with a lot of acceptance tests which didn’t pass. Our objective was to use TDD and write code to make the acceptance tests pass. Every change we made to the game code had to be as an effect of creating a unit test that failed.

I’ve personally been writing unit tests for two years now, but haven’t done any real TDD until very recently. So I’m still in the process of re-learning the way to write code. In TDD you should “Write the test that force you write the code you want” as Robert so elegantly put it. More formally there are three rules of TDD which are:

  1. You are not allowed to write any production code unless it is to make a failing unit test pass.
  2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
  3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

If you practice TDD properly you should, accordingly to Robert, produce more than 1000 unit tests per year. That is a lot more than what I’ve produced my last year! And then I actually thought that I had been writing a lot of tests.. But when you think about it, it’s not really an unreasonable amount. Knock it out over a year and it’s just about 4 tests per day. Now it didn’t sound so much, did it?

Throughout the day, Robert interrupted regularly to ask the audience questions and share his opinions about difference aspects of software development. I will present some of the nuggets here:

“Comments are dirt in code”. My favourite! What he means is that most of the time comments in code try to make up for poor design. If the design was really good, the comments wouldn’t be needed. I really agree with this reasoning when it regards that comments shouldn’t be needed inside code blocks. However, public comments that clarifies, for example what state a parameter must have (not null is common), I still think are needed sometimes, especially for APIs where you don’t have access to the source code.

“Implement new functionality in priority order, not what is convenient from a design point-of-view”. This sentence stuck since we’ve been discussing this lately at the company I work for. The reasoning behind it (besides the obvious point that high priorities are more important than low priorites) is that the design should be refactored continually so that the best possibly design will emerge independently of the order that the tasks are implemented.

He also gave so guidelines of method design. A well-designed method should:

  • not be longer than 5 lines of code
  • not have indentation depth (like for and if statements) of more than 2
  • not have more than 2-3 parameters

Personally, I’m not quite there yet when it comes to the first two statements. I try to make my methods short, but sometimes they are up to 30 lines of code.. And my indentation depth often goes up to three or sometimes even four.

At the end of the tutorial, there was time for reflections of working with TDD and a question from the audience was that they had gotten redundancies where some unit tests were almost identical as some of the acceptance tests. This was okay, according to Robert.

On the negative side I don’t have much to say.. Perhaps only that he at one time used exception handling for taking care of bad user input from the command line. In my book*, exception handling should only be used for exceptional events, not such a common case as when a user misspells a command..

All in all, I found the tutorial very giving and inspirational. How much the tutorial will affect my development process, I don’t know yet.. But I think I’ve gotten enough knowledge and energy to really start doing real test-driven development from now on. It will be fun to reflect upon this in a couple of weeks.

* No, I haven’t written any books. I mean the figure of speech.

Categories: TDD Tags: