How can you learn test-driven development (TDD)? I could explain the principles and practices of TDD. And yet the question that comes back is, “But what do I actually do in Xcode?”
That’s what code katas are for. They’re not tutorials. They’re exercises. Let me introduce you to a few that are set up to go in Swift. Then you can try doing one today.
(I’m in the middle of rewriting this post, so it’s “under construction.” Instead of focusing solely on the Bowling Game, I list a few different exercises. Then we’ll drill into the Bowling Game.)
What’s a code kata?
How do professional athletes stay on top of their game? They practice. How about professional musicians? Practice, practice, practice.
“Kata” is a Japanese martial arts term for choreographed patterns of movement. They’re also called “forms.” Both students and masters practice these detailed patterns over and over. The movements can come without thought, because your body knows what to do.
A “code kata” applies this idea to coding. It’s a self-contained exercise you can repeat. Every time you repeat it, you’ll learn something new. At first, you’ll learn one approach to solving a problem. As you repeat the same approach, your learning will shift toward muscle memory.
Then you can tackle the same problem, but try a different approach. The style used across the world on the Global Day of Coderetreat is to repeat a problem, but with different coding constraints. For example, can you code without your mouse? Can you code without queries? …A physical analogy would be running with extra weights, or fighting with one hand.
Doing self-contained exercises like this is the best way to practice coding. They’re outside of your production code, so there’s no worry of breaking anything. You’ll throw away what you try, because you’re going to repeat the coding kata again later. The goal is to gain mastery over coding tools and techniques.
Some use the term to refer to coding puzzles in general (how would you code this or that). But let’s follow the martial arts metaphor to see where it may take us. A code kata can be a set of moves, memorized and practiced until they flow effortlessly.
So a “TDD kata” is designed to train your TDD muscles. The Bowling Game code kata is designed to impart the moves of test-driven development. I’ve taken the original presentation and created a version showing these moves in Xcode, using either Swift or Objective-C.
The importance of a notepad in TDD
Before you start: Make sure you have something to write with and a piece of paper. An index card or sticky note is all the space you need.
In the upper-left corner of the slides, you will see a box with notes that looks like this:
Every time you see something there, jot it down on your paper.
This is important! TDD is a discipline of doing one thing at a time, so when we notice something to clean up, we write it down for later. When you are done with a clean-up item, cross it out.
TDD Kata: Specific moves for the Bowling Game slides
Download the version you want:
Green lines show code that was just added. Red lines show code that is about to be removed. In this example, we added the last line, and are about two remove the two lines above it:
Let me call out some specific slides…
Slide 12: Set up your Xcode display to mimic the slide. Do this as follows:
- Swift: Click the test file so it shows on the left. Option-click the implementation file so it shows on the right.
- Objective-C: Click the test file so it shows on the left. Option-click the .h file so it shows on the right. Shift-option-click the .m file and double-click the “+” in the bottom right. On the right side, you should have .h above and .m below.
Slide 23: Select the two circled lines, then try to use the contextual menu to Refactor → Extract. (If this causes Xcode to crash, just do it by hand.)
Slide 40: Rename loop variable “i” in one shot by selecting its first appearance and doing Edit All in Scope. This is available through the menus, or in a little pop-up menu that appears when you hover over a selection, as shown here:
If you’re doing this by yourself, I’d recommend going through the entire TDD kata, once through.
Then following Marlena Compton’s advice in Learning TDD with Exercises, do 15 minutes a day. Set a timer. Stop when you get to 15 minutes. The next day, continue from where you stopped.
Question: How did it go? What questions do you have? Leave a comment below.
For even more “flow,” get the Xcode code snippets I use as to generate the outlines of test cases.