Xcode TDD Kata: Practice with the Bowling Game

August 28, 2011 — 11 Comments

February 22, 2016 Update: Added Swift. Refreshed Objective-C too, for latest Xcode.

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?”

That’s what the Bowling Game TDD Kata is for. There’s an Objective-C version and a Swift version.

Xcode TDD Kata

Highkick by Pandiyan V, used under CC BY-NC 2.0 / Added text to original

What’s a code kata?

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

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:

  • 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.
  • Swift: Click the test file so it shows on the left. Option-click the implementation file so it shows on the right.

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

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. Do this daily, for a month.


Ready? Download the Bowling Game TDD Kata for Objective-C or Swift 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 test code templates.

Jon Reid

Posts Twitter Facebook Google+

I've been practicing Test Driven Development (TDD) since 2001. Learn more on my About page.

11 responses to Xcode TDD Kata: Practice with the Bowling Game

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

    • Robert,
      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?

  2. Daniel Stokowski January 10, 2014 at 1:10 am

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


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

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

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

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>