Back to the Ruby Koans today and we got set the task of completing them all by the end of this week. For those who haven’t heard of them, Ruby Koans are a great intermediate/advanced introduction to the Ruby language. It covers all the basic stuff like arrays, symbols, methods, but rather than deliver the content to be learned passively (i.e. by reading it), it uses tests which you have to make pass to get to the next exercise (Koan) in the set.
The method of education used by the Koans, a test-based approach, is intentionally in-line with the prevailing software development methodology used today – namely, Test Driven Development or TDD. The idea behind TDD is to write tests before you write any feature you want to implement.
Ok, so how does that work in practice?
For example, if I want to add a feature to my program that adds an array of numbers together, rather than jump head first into trying to write code that implements this feature, I first write a test (or for more complicated features, a series of tests) which will check if my code does what it is supposed to do.
I then run the test and watch it fail. The test will fail because I haven’t written the code to implement the feature yet.
I then write the code which makes the test pass. I refactor the code. Repeat until all applicable tests pass.
******************************************************************
Why bother with TDD?
I agree that TDD can slow you down in some instances, such as when you’re writing trivial code like a feature to add two numbers together. However, with more complex problems, TDD requires you to take a step by step (or rather, test-by-test) approach which helps you break the problem down into manageable chunks, and it also encourages early bug detection in your code. Better to detect bugs in your code ASAP, rather than 5 months down the line when the code base has increased by a factor of 10!
So doing the Koans today really hit home how important TDD is to writing code that works. I’d been stuck on a particular exercise when I attempted the Koans a few weeks ago (it was on our pre-reading list) but once I started writing code which focused on making each test pass, I
a) stopped trying to figure out what ALL of my code was going to look like before writing it
and
b) managed to get onto the next exercise!
One last thing to note is that although there is an online version of the Koans available, it really is worthwhile (and easier) to download the code and run it on your own computer. Firstly, because you’re able to format your code so that it is easier to read (no tabs in HTML input forms) and second, because it replicates and encourages realistic TDD practices – run test, watch it fail, write code to pass the test, repeat.
******************************************************************
I stayed a little later tonight to finish all of the Koans – what can I say, I was encouraged by my little piece of success – and so will get a chance to apply some of my Ruby knowledge to figuring out actual problems on my own this week. As it always is with languages (both regular and programming), my reading/understanding of other people’s code far outstrips my writing ability.