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

Triangulation: Adding Tests to Drive Generalization

Learn how writing multiple test cases forces you to replace specific implementations with general solutions.

Triangulation: Adding Tests to Drive Generalization

You'll learn how writing multiple test cases forces you to replace hard-coded values with real logic.

The Core Idea

When you first make a test pass in TDD, the simplest solution is often to return a hard-coded value. That's perfectly fine! But how do you know when to write actual logic instead of cheating? Triangulation is the technique of adding more test cases with different inputs until the hard-coded approach breaks, forcing you to generalize.

Think of it like GPS triangulation: one signal isn't enough to pinpoint your location, but multiple signals from different directions reveal where you truly are. Similarly, one test isn't enough to reveal the general algorithm—you need multiple examples to "triangulate" toward the real solution.

How It Works

  1. Write a test with specific input/output
  2. Make it pass with the simplest code (often hard-coded)
  3. Write another test with different input/output
  4. Now your hard-coded solution fails—you must generalize

For example, if you're building an add function:

  • First test: add(2, 3) should return 5
  • Simplest solution: return 5
  • Second test: add(1, 1) should return 2
  • Now return 5 fails—you must write return a + b

When to Use It

Triangulation is most valuable when you're uncertain about the algorithm. If the solution is obvious, skip straight to the general implementation. Use triangulation when you need examples to guide you toward the pattern.

Key Takeaway: Triangulation uses multiple test cases with different data to force yourself from specific, hard-coded solutions toward general, algorithmic ones—letting the tests reveal the true implementation.