How do you learn Test Driven Development? I could explain the principles and practices of Xcode TDD, but the question that comes back is, “But what do I actually do in Xcode?”
“Kata” is a Japanese martial arts term for choreographed patterns of movement. Also called “forms,” both students and masters practice these detailed patterns over and over, so that the movements can come without thought.
A “code kata” applies this idea to coding. Some use the term to refer to coding puzzles (how would you code this or that) but let’s be faithful to the martial arts metaphor. A code kata is a set of moves, meant to be memorized and practiced until they can flow effortlessly.
So a “TDD kata” is designed to train your TDD muscles. “Uncle Bob” Martin designed the Bowling Game Kata to impart the moves of Test Driven Development. I have taken his presentation and created a version showing these moves in Xcode, using either Objective-C or Swift.
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: Currently Objective-C only… 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: Both Objective-C and Swift! 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.
Then following Marlena Compton’s advice in Learning TDD with Katas, do 15 minutes a day. Set a timer. Stop when you get to 15 minutes. Try doing this daily.
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 test code templates.
Jon is a consultant on Clean Code for iOS, focusing on Test Driven Development, unit testing, refactoring, and design. He's been practicing TDD since 2001. You can learn more about his background, or see what services he can bring to your organization.
Please log in again. The login page will open in a new window. After logging in you can close it and return to this page.