Michael Feathers and I competed in the “Programming with the Stars” competition today, which was fun, if rather nerve racking. Our category was “TDD from scratch” and we chose to perform the FizzBuzz Kata in python. We chose that because we thought it was simple enough we could do the whole thing in the time limit, just four minutes (!).
It seemed to go smoothly, ping-pong programming, and this is the code we ended up with:
def fizzbuzz(max): return ["FizzBuzz" if i % 15 == 0 else "Buzz" if i % 5 == 0 else "Fizz" if i % 3 == 0 else i for i in range(1, max+1)] assert fizzbuzz(1) == [1] assert fizzbuzz(2) == [1,2] assert fizzbuzz(3) == [1,2,"Fizz"] assert fizzbuzz(5) == [1,2,"Fizz", 4, "Buzz"] assert fizzbuzz(15)[-1:] == ["FizzBuzz"] print "\n".join(map(str, fizzbuzz(15)))
As you can see we didn’t bother with a testing framework of any kind, just a few assert statements, and the code that does the work is all one line, (although we put in some line breaks to make it readable). I was pretty pleased we managed to do it without mishap.
The judges turned out to be Bob Martin, Rachel Davies and Mike Hill, and they weren’t actually as nasty to us as I feared. We got good marks from Rachel and Mike, (8/10 from both), and since Bob was mean to nearly everyone, his 5/10 was actually ok. Only one other pair scored more than us, which puts us through to the next round tomorrow. Our category is going to be “Refactoring”, which is basically Michael’s specialist subject, so hopefully we’ll be able to come up with something good.