Skip to content

Write less Swift code and TDD quicker

If you’re finding TDD is slowing you down then this article will help you turn the tables and make TDD quicker.

Let’s remind ourselves of the three rules of TDD:

  1. You are not allowed to write any production code unless it is to make a failing unit test pass.
  2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
  3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

If you’re not familiar with these rules, be sure to read this.

“You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures”

Let me get this straight. So. You’re not allowed to create a class until you have a test to allow you to write that class. And when you want to add a new method, you can’t write that method until you have a test to allow you to write that method.

This might seem like a slow way to write code but TDD should not be slowing you down. In fact, it should do the opposite; you should be finding TDD quicker than writing the tests after.

One way to help speed up TDD is to use good tools and AppCode helps us speed up by enabling us to create code from usage. If you don’t know what that means yet, you’re in for a treat.

Step 1 – Write a test class for a new type

If AppCode doesn’t know what a type is it will offer to create it for you. Place your cursor over the unknown type and press ⌥⏎. You can create the new type in the same file, nested in the current type, or in a new file.

In this example I want the type in a new file.

Generate class from usage too TDD quicker

Step 2 – Write a test for a new method

Just like an unknown type, AppCode will offer to generate a method for you if it doesn’t already exist. Same as before: place your cursor over the unknown method and press ⌥⏎.

Notice how AppCode generates the correct method signature and even the correct parameter type. Pretty cool, right?

Generate method from usage to TDD quicker

You just made TDD quicker ✊

If you don’t already write your tests first then generating code like this is a great incentive to start. It will save you so much time jumping between files and copying and pasting method names that you’ll wonder how you ever managed without it.

The less code you have to write, the faster you can develop and the more you can focus on the things that matter. So stay away from ⌘C and ⌘V and say hello to ⌥⏎.

Extra tips

  • You can also create local, instance and global variables from usage.
  • AppCode also generates protocols, structs and enums.
  • When you choose a group for your new type, AppCode will automatically put it in the correct folder.
  • If you decide to add a parameter to an initialiser, AppCode will offer to create that for you too.
  • AppCode recognises method parameter labels.
  • When you create a method that returns something, AppCode will implement the return statement and even guess what to return if it can.
  • You can create a class from usage without using your mouse to be extra speedy.
  • When browsing groups, start typing the name of the group where you want to save your new class to find it quickly.

More on TDD

Writing the tests first has many benefits and it’s important to follow this discipline. If you’re yet to be convinced, then get reading!

Subscribe now to hear more about AppCode tips and tricks

Published inAppCodeiOSTDD

Be First to Comment

Leave a Reply