Quality Coding
Emoji: Bowling

Xcode TDD Kata: Practice with the Bowling Game


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

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:

Example of notes on slide

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:

Green and red annotations

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:Edit All in Scope in Xcode

Daily practice

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.


Ready? Download the Bowling Game TDD Kata for Swift or Objective-C and start practicing Xcode TDD!

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.

About the Author Jon Reid

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.

follow me on:
  • Robert Walsh says:

    Hi Jon,
    I really enjoyed the kata, and I appreciate the work you’ve done to explain the mechanics of doing TDD in Xcode. Also, thanks for making the improved template available.
    On Slide 38, why do you immediately recognize the flaw in the design and refactor it then and there? At that point in the project, the design seems perfectly suitable for the tests that have been written, and a new test (like testOneStrike) would highlight the flaw and force the refactoring.

    • Jon Reid says:

      I’ll try to explain Slide 38, but need to back up. testOneSpare was introduced back on Slide 29, as a failing test. In the process of trying to think about what to code to get it to pass, we discovered deficiencies in the design. So we comment out testOneSpare in order to concentrate on refactoring — wearing one hat at a time.
      As of Slide 36, the refactoring has been proved successful. So I re-enable testOneSpare, and run it to see that it’s still a failing test.
      On Slide 38, I’m back at the point of trying to code something that’ll pass testOneSpare. This is the second try. Again, I discover a deficiency in the design. So once again, I switch hats by commenting out the test, concentrating on tweaking the design (refactoring) in a way that doesn’t break the previous tests.
      This illustrates the principle of wearing one hat a time. Either you’re coding to get a new test to pass, or you’re refactoring. But don’t do both at the same time. We’re basically walking on stilts: Lift one, or the other, but not both. :) By keeping one foot grounded, so to speak, we reduce the risk of falling over.
      Does that makes sense?

  • Daniel Stokowski says:

    Hello Jon.
    Thanks for great excercise. It’s awesome! I practised it yesterday and have some thoughts:
    – Apple template looks almost like yours now (XCode 5), and it’s easier to use it than run yours (I couldn’t manage it to work – tests wasn’t being run)
    – Last test was successful without any changes in code (code from previous test was enough) – is that ok or I missed something?

    If you want I can send you detailed description what went different than in yours PDF so you can update it for XCode 5.

    Have a nice lecture here in Poland. Sad that I missed opportunity to be there. :(


    • Jon Reid says:

      Daniel, I’m glad you found it helpful.
      – Yes, Apple’s latest template finally addresses my concern. You no longer need my template, unless you want to use the older SenTestingKit.
      – The last test is a surprise: it basically shows that there’s nothing left to do! But it’s hard to tell without a test to prove it.

      Polish hospitality is wonderful! I had a great time, and look forward to going back.

  • Justin Stanley says:

    Hi Jon! I’m just starting this using the Swift version…I noticed on slide 11 it says “Select Objective-C as the language.”, but it should say Swift.

  • I finished going through it and here’s what I noticed could be tightened up, if you want. ;)

    Slide 11

    – It should say “Select Swift as the language” instead of Objective-C. (error)

    Slide 12

    – there is a minus sign in front of fun testGutterGame() that I think is leftover from Obj-C? (error)

    Slide 15

    – the return line in func score() doesn’t need the semicolon (unswifty)

    Slide 20

    – theScore variable doesn’t require “: Int” since it’s implied in Swift (unswifty)

    Slide 27

    – the semicolons in the new test should be removed (unswifty)

    Slide 28

    – the red comment on the bottom should say 16 instead of 20 (error)

    Slide 32

    – rolls variable doesn’t require “: [Int]” since it’s implied in Swift (unswifty)
    – currentRoll doesn’t require “: Int” since it’s implied in Swift (unswifty)
    – currentRoll++ should be changed since it’s deprecated very soon in Swift (deprecated)
    – Change to:
    rolls[currentRoll] = pins
    currentRoll += 1
    – the return of func score() was changed from theScore to score but that line isn’t green

    Slide 47

    – the strikeBonus method has a minus sign in front of private func (i.e. -private func) (error)

  • Paul Booth says:

    Hi Jon,

    Thanks for creating these iOS versions of Uncle Bob’s Bowling Game Kata. I’ve just tried the swift one and I noticed that the following line –

    for _ in 1...10 {

    on slide 37 (The Third Test) throws an ‘Index out of range’ exception.

    It should, of course, be

    for _ in 1...9 {

    which is the equivalent of the java code from Uncle Bob –

    for (int frame = 0; frame < 10; frame++) {



  • Paul says:

    You’re right Jon. I’d was accidentally counting the frames from 0 to 10 on my first day

    for _ in 0...10 { // Doh!

    but, as you say, it makes more sense to count the frames from 1 to 10 (rather than 0 to 9).

  • >