How can you learn Test-Driven Development? 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.)
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.
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.
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:
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.
Programming was fun when I was a kid. But working in Silicon Valley, I saw poor code lead to fear, with real human costs. Looking for ways to make my life better, I learned about Design Patterns, Refactoring, and Test-Driven Development (TDD). Programming became fun again! I've now been doing TDD in Apple environments for 18 years. I'm committed to software crafting as a discipline, hoping we can all reach greater effectiveness and joy.
Please log in again. The login page will open in a new tab. After logging in you can close it and return to this page.