Course contentsShow
← Software Engineering
Lesson 505 of 1,93315. Test-Driven DevelopmentFree lesson

Dealing with Uncertainty in TDD

Use spikes and exploratory code outside TDD when facing high uncertainty, then apply TDD to the known solution.

Dealing with Uncertainty in TDD

You'll learn when to step outside the TDD cycle to explore unfamiliar territory, then return to disciplined test-first development.

The Challenge of Unknown Terrain

TDD works beautifully when you understand the problem and have a clear path forward. But what happens when you're facing a brand-new API, an unfamiliar library, or a problem you've never solved before? Writing tests for something you don't understand yet feels like drawing a map of a place you've never visited.

Spikes: Exploring Before Committing

When uncertainty is high, use a spike—a short, time-boxed exploration where you write throwaway code without tests. Think of it like a scout venturing ahead to survey the landscape before the main expedition follows.

During a spike, you:

  • Experiment freely with the API or approach
  • Learn how things actually work (not how you assume they work)
  • Identify the core challenges and edge cases
  • Prototype solutions without the constraint of testing

Crucially, spike code is disposable. It's learning code, not production code.

Returning to TDD with Knowledge

Once your spike reveals the path forward, delete or set aside the spike code and restart using TDD. Now you understand the problem well enough to write meaningful tests first. Your red-green-refactor cycle will be confident and purposeful because you've eliminated the uncertainty.

This isn't cheating—it's recognizing that TDD assumes you know what "correct" looks like. When you don't, exploratory code helps you learn quickly, then TDD helps you build reliably.

Key Takeaway: When facing high uncertainty, use time-boxed spikes to explore and learn, then return to disciplined TDD once you understand the solution space.