Top-down TDD for Clojure
I’ve recorded a 43-minute screencast. It’s a decent example of my current style of TDD for Clojure. It emphasizes:
-
The use of Midje prerequisites (mocks) to support top-down development.
-
Using metaconstants to make tests more abstract and readable.
There are two sources for the video:
- Vimeo: http://www.vimeo.com/19404746
-
Download: https://github.com/marick/Midje-videos/raw/master/robozzle.mov (If that fails with “blob too big”, go here and select Downloads:
You’ll get a zip file containing the video in its own directory.
UPDATE
Alert watcher Mark Derricutt notices that I got east and west mixed up. I, um, I… I did that deliberately (no, really!) to illustrate an important point about TDD. TDD helps you do what you intended. It is not particularly good at helping you intend the right thing. Indeed, the flow experience it can induce probably makes you worse at noticing you’re proceeding smoothly in the wrong direction.
For example, I had a niggling feeling throughout that I was getting the orientation wrong. That, plus general principle, made me shy away from just copying the answers from the tests into the rotation maps—instead, I recreated them. All for naught: the incorrect mental image I used when I calculated the first left turn had captured my mind.
This getting stuck in a rut is one of the reasons why you still need testing with a fresh mind (either someone else’s or your own, after a rest).
It’s interesting to speculate whether this bug would ever become visible. If, just after finishing the kata, I’d wired up a UI and chosen four ship images, I’d probably again get east and west flipped, meaning all would look well to the user. It’d be like using y
as the variable representing horizontal position and x
for vertical position: functionally correct if you do it consistently, but death to any sane mind reading the code.
If I chose ship images later, I’d be more likely to get them right (that is, wrong given the bug). I do at times know east from west.