How can you learn and practice test-driven development (TDD)? I can explain the principles of TDD. But 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, designed to help you grow in your technical agility. I’ll introduce you to some that are specifically for Swift programmers. Then try doing one today!
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 beginners and masters practice these detailed patterns over and over. The movements eventually 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.
TDD katas in Swift: 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 with different approaches. A common style during the annual Global Day of Coderetreat is to repeat a problem while adding different coding constraints. For example, can you code without your mouse? Can you code without exposing properties? How about no conditionals? …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 later. The goal is to gain mastery over coding tools and techniques.
So let’s look at some exercises for Swift programmers. But first, here are a few pro tips.
TDD Kata Pro Tips
Use a Scratchpad
Before you start: Make sure you have a piece of paper and something to write with. An index card or sticky note is all the space you need.
Why? Because TDD is a discipline of doing one thing at a time. When you notice something to clean up, don’t dive straight into it. Write it down for later, and stay focused on whatever you’re working on now.
When you complete a task, revisit your list. Clean up the code, and cross the entries off your list.
In the Bowling Game example, this scratchpad is actually shown in the slides. In the upper-left corners, you’ll see boxes like this:
When you see this, write it down on your scratchpad. I mean it: write it down. This will help reinforce good TDD habits.
When you’re working on a new code kata, I’d recommend going through the entire exercise, once through.
Then we can follow Marlena Compton’s advice in Learning TDD with Exercises, by doing 15 minutes a day. Set a timer. Stop when you get to 15 minutes. The next day, continue from where you stopped. Try making it a small part of your daily routine.
In the spirit of using different constraints, work through a TDD kata without them. Then do it again, using my templates. See if they change the experience.
TDD Katas in Swift
The idea is to score a game of bowling. Follow along with slides:
In my version of the Bowling Game slides, green lines show code that we just added. Red lines show code that we’re about to remove. For example, here 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 Xcode to show test code and production code side-by-side, mimicking 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.
You may prefer to use tabs instead, and that’s fine. The goal is to see the code side-by-side, or switch between them so fast that it doesn’t matter.
Slide 23: Select the two circled lines, then use the context menu to Refactor → Extract. (If Xcode refuses, do it by hand. Or, try AppCode which has better automated refactoring.)
Slide 40: You have various options to rename loop variable “i” using Xcode:
- Do it by hand.
- Use “Refactor → Rename” observing how long it takes to index and animate.
- Select its first appearance and do Edit All in Scope. This is available through the menus, or in a pop-up menu that appears when you hover over a selection, as shown here:
Try each option to see how they affect your “flow.” And one more option is to try AppCode.
Not a TDD kata, this is a legacy code refactoring kata. You start with a glob of untested code. Your challenge:
- First, bring the code under test.
- Clean it up. The goal is to make it easy to make the change below. Can you move in small, verified steps?
- Then add a new feature: “Conjured” items should degrade in Quality twice as fast as normal items.
The challenge is to input a name in standard English order, returning the name normalized for use in a bibliography.
This is a “TDD paint by numbers” exercise: the tests are written for you. Initially, they’re all disabled. Enable them one-by-one, and follow the remaining two steps of the TDD Waltz:
- Write enough production code to pass the test.
- Refactor the code so it expresses the domain. (In this exercise, leave the test code alone.)
This kata is available in several languages, including Swift. If you check out the swift-solution branch, you can follow one possible solution as a series of commits.
I also offer 75-minute mob programming sessions in this exercise on PubMob.
I offer this kata in the last chapter of my book, iOS Unit Testing by Example.
Make code to greet someone by name, according to the time of day:
- 5:00 am – 11:59 am: Good morning.
- 12:00 pm – 4:59 pm: Good afternoon.
- 5:00 pm – 4:59 am: Good evening.
So if we provide the name “Lauren” and the time is 4:15 p.m., it should return, “Good afternoon, Lauren.” Allow for the possibility that there is no name.
Do you have other TDD katas in Swift that you like? Let me know, and I’ll add them to the list.