So you want to try Test Driven Development — great! But that requires expressing the intent of the code-to-be in automated tests. Where should you start if you don’t even know what the code should do? What should you do if you’re not confident about a particular approach?
You drive a spike through it!
To start my “Marvel Browser” iOS TDD sample app, I want to begin by making a network call to the Marvel API. If I knew what I was doing, I would start the 3-step “TDD waltz” of writing a single failing test, then writing the simplest code that passes, then refactoring.
But there are two roadblocks keeping me from writing my first test:
When you’re dealing with an unfamiliar framework call, or an unfamiliar sequence of calls that needs to work end-to-end, TDD is not the place to start. Instead, you need a “spike solution.”
A spike solution is a code experiment. It’s called a “spike” because instead of carefully building things up layer by layer, we just brute-force something, driving through multiple layers.
A spike solution is dirty & quick. Don’t spend time making it right. Instead, code just enough to get the answer you need.
Sometimes you can create a spike in a brand-new, throwaway project. But often, I find it easier to use my project’s existing infrastructure to set up the scenario I need. In that case, put the spike in a branch.
You can tinker with your spike, hacking any way you please. You can study what you come up with. But you must resist the temptation to start copying the hack over to your production code. Resist!
In fact, once you’re done with a spike solution, throw it away. There are good reasons to do this:
Disclosure: The book links below are affiliate links. If you buy anything, I earn a commission, at no extra cost to you.
Here’s how The Art of Agile Development by James Share and Shane Warden answers the question, “Should we really throw away the code from our spikes?”
Unless you think someone will refer to it later, toss it. Remember, the purpose of a spike solution is to give you the information and experience to know how to solve a problem, not to produce the code that solves it.
As I explained in TDD Sample App: 20 Topics that May Spill Out, the main thing I want the Marvel Browser to do is send a name prefix and get back comic book characters. Again, there are two things I’m uncertain about: how the NSURLSession works, and how Marvel does request authentication. So let’s spike it in part two: Spike Solutions: 7 Techniques You Can Use.
When have you used spike solutions? Did you throw them away? You can leave a comment by clicking here.
Please log in again. The login page will open in a new window. After logging in you can close it and return to this page.