Can you write good code without testing or running it? Can you refactor without even compiling the code? Can you reason about code that only exists as a text document?

Earlier this week I wrote a blog post about doing TDD only in your head, without writing anything down. I called it “TDD like Mozart” after an idea J. B. Rainsberger had in a Tweet. It was a fun exercise to do, but it has some drawbacks:

  • Overly strict rules: I could not correct any errors I spotted, because I did not allow myself to change lines already written.
  • Because of the same rules, I could not remove duplication I spotted when I wrote the code down
  • The exercise can only be done alone

Note that the first two drawbacks are actually features of the exercise. You should do all the refactoring and syntax corrections in your head.

Mozart was a great composer who did extraordinary things. Beethoven was another great composer who did extraordinary things.

Enter "TDD like Beethoven"

I decided to change the rules of the exercise a little bit. The new rules should make it easier to correct small mistakes and to work on the exercise together.

In about 1800 his hearing began to deteriorate, and by the last decade of his life he was almost totally deaf. He gave up conducting and performing in public but continued to compose; many of his most admired works come from this period. Ludwig van Beethoven The Symphony No. 9 in D minor, Op. 125 (sometimes known simply as "the Choral"), is the final complete symphony of Ludwig van Beethoven. Completed in 1824, the symphony is one of the best-known works of the Western classical repertoire. Among critics, it is almost universally considered to be among Beethoven's greatest works, and is considered by some to be the greatest piece of music ever written. Symphony No. 9 (Beethoven)

What is the equivalent of “being deaf” in programming? Beethoven could not hear how his work really sounded. He could not experience his work. So, in this exercise, you are not allowed to compile or run your code. But you can - and should - write it down. Reason about it. Move stuff around. Just don’t compile or run.

The Rules

  • Find a small problem you want to solve in code
  • You can use all the documentation you need
  • You can use a text editor with syntax highlighting, but...
  • Do not compile or run the solution! Do not use auto-complete!

“Compose” the solution:

  • Come up with a test and write it down in the text editor. Reason about why it must fail right now.
  • Write down the code that will make the test green. Explain to yourself why it will make the test pass.
  • Double check for syntax errors, exceptions, ...
  • Refactor the code. Explain to yourself why the refactoring is better. Explain why all the tests still pass.
  • Double check for syntax errors, exceptions, ...
  • Repeat until finished.

Run and correct mistakes:

  • Compile and run
  • Change the code so it runs and compiles
  • Write down all the changes you had to make

I guess in this exercise it will be a little bit easier to get to a running program before the first compile than in “TDD like Mozart”. But I don’t think the exercise will be easier overall, because this raises expectations. And it will still be really hard. But at least it will be more fun to do as a pair exercise than “TDD like Mozart”.

Side note: For now, I will only write down the rules because I don’t have time to run the exercise right away. I will try it out later, and I’ll probably record a screen cast while doing so.

What do you think of this exercise? Are you thinking about trying it? Contact me and tell me about your experiences. If you blog about this exercise, I will gladly link your blog post here and re-tweet it. All my contact details are at the bottom of the page.