Saturday, October 03, 2009

TDD Kata: Do not attempt this alone.

There has been some talk of TDD Katas over the last couple of days on Twitter that I think originated from something Roy Osherove did in his TDD Masterclass. I find the concept intriguing.

So what is Code Kata?

A code kata is a set of programming exercises designed to improve your coding skills through pratice and repetition.

Dave Thomas of Pragmatic Programmer fame describes it as such:
How do you get to be a great musician? It helps to know the theory, and to understand the mechanics of your instrument. It helps to have talent. But ultimately, greatness comes practising; applying the theory over and over again, using feedback to get better every time.
and as a failed musician (through lack of practice) I see sense in it. When you learn to play a piece of music you don't just play it all the way through. You practice parts of it. To attain the skill to play parts of the piece you learn scales or chords and practice them on their own.
So I thought I would have a go at Roy's TDD Kata.

30 mins later

Finished. Would you believe I finished within 30 mins? (Note: my 1st attempt at this kata was a cutdown version but I crammed everything into the allotted time, just!
That leads me to one conclusion, I MUST BE AWESOME.
So there we have it. I've'done' Katas.
In the next blog I'll be talking about

But what exactly did you do?

Oh yeah what did I produce? Well I got it reviewed by someone hot out of Roy's TDD course and it had a number of issues. It seems that over the years I have picked up a couple of bad habits. It was like I was playing the tune but holding the instrument upside down. So I swapped reviews with the reviewer and attempted to try again using the feedback I was given.

I didn't even get halfway through it! :(

This is great I've got lots to learn now. I can improve my process and really learn how to Test Drive my code quickly and effectively and also improve on the actual code itself.

But then I read Roy Osherove's blog entry on the kata and I'm confused. Roy says:
The point about the Kata is that you should know it by heart. you should not be thinking about the solution, your fingers should by typing it "blind", including all the refactoring work, the renaming and whatever else you do.
So am I blurring the intent of the kata? By reviewing the code and improving it am I breaking the ethos of the kata? If the pupose of the kata is getting to know your tools and output better, then surely the most productive way is to have snippets at every section. Or to be utterly ridiculous to just copy paste the code in.
The idiot programmer like me will just be practising its awful solution to the problem and will be blissfully ignorant in the fact that it is improving its craft. And yes it may be able to do it in under the allotted time but when someone else comes to pick up its code, they may well spend a lot longer sorting out the unorthodox way it was done.
This is obviously wrong.

Not a solo activity

Its plain to see that you need a someone else to see where you are going wrong. Where you can improve even if that someone else is you! Do a retrospective, read a book on "cleaner code" or perhaps on the "art of unit-testing"? (No books spring to mind right now - I'm sure a web search may bring some up though).
If you do have the luxury of another person be subjective about their critique, they may have things you disagree with but try not to take it personally.

An improved kata?

I love the idea of the kata but I can see where I could go horribly wrong.
What I would like is the idea of a community kata. A bunch of developers do single kata over a fixed period, say a month. Over that month each time they complete the kata they commit it to a version control system where the other devs can access it and review the code. An annotation of tips and hints (e.g. was mouseless, used this snippet) is included in the commit notes so everyone can compare techniques. Time to complete would be a nice to have but more importantly an indication of improvement of time would be preferred. My single finger seeking typing my not be 150wpm but I if improve by 20% that would indicate my other methods are working. At the end of the month the developers can choose to stay on or try another.

I'll be doing more kata as I think it's a great concept in the right circumstances. You want to improve your ability to use your tools but if your music is crap...


Anonymous said...

A book about cleaner code... how about Clean Code by Robert C. Martin? Only a few chapters in yet, but so far very good stuff :)

Johnno Nolan said...

Svish - yeah I was being a little silly there. I was thinking fo 2 books "Clean Code" and "The Art of Unit Testing" by Roy Osherove