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 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 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. 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.