tezvyn:

Describe the Red-Green-Refactor cycle in TDD. What is each step's purpose?

AI-drafted, machine-checkedSource: martinfowler.combeginner

This checks if you understand TDD as a design discipline, not just testing. A strong answer maps Red to a failing test defining the interface, Green to minimal passing code, and Refactor to cleaning both codebases without changing behavior.

WHAT THIS TESTS: This question probes whether you see Test-Driven Development as a design discipline and not merely a way to write tests. Interviewers want to know if you understand the behavioral loop that Kent Beck defined in Extreme Programming and whether you can articulate why each phase exists beyond the surface-level color names.

A GOOD ANSWER COVERS: A good answer hits four things in order. First, Red means writing a failing test for the next small bit of functionality you want to add; the purpose is to define the desired behavior and the interface before any implementation exists, which forces you to separate interface from implementation. Second, Green means writing the simplest functional code that makes that test pass; the purpose is to get rapid feedback and a working baseline, even if the code is initially ugly or naive. Third, Refactor means cleaning up both the new production code and the test code to make them well structured without changing any behavior; the purpose is to keep the design clean and avoid a messy aggregation of code fragments. Fourth, a strong candidate mentions the hidden setup step of creating a prioritized list of test cases first, then picking the next test that drives the design toward the salient points.

COMMON WRONG ANSWERS: The most common wrong answer is skipping the purpose of Refactor and treating TDD as test-after development with extra ceremony. Another red flag is saying Red is just to prove the test harness works; while a failing test validates the harness, its real purpose is to drive interface design. A third red flag is describing Green as writing fully featured or optimized code; the goal at that stage is only to pass the test, not to build the final design.

LIKELY FOLLOW-UPS: An interviewer might ask how large each step should be, which lets you discuss working in tiny increments of a few minutes each. They might ask how TDD affects design, where you can explain that thinking about the caller first reduces coupling and improves cohesion. They could also ask what you do when legacy code has no tests, which opens a discussion of characterization tests and the seam model.

ONE CONCRETE EXAMPLE: Imagine adding a discount calculator to an e-commerce system. In the Red phase, you write a test asserting that calculateDiscount returns 10 percent when the cart total exceeds 100 dollars; this immediately forces you to decide the method name, parameters, and return type. In the Green phase, you hardcode the return value of 10 just to make the test pass, because the goal is only to get to a passing state. In the Refactor phase, you extract a constant for the threshold, rename variables for clarity, and remove duplication between this test and existing ones, leaving the code clean before you move to the next test case.

Read the original → martinfowler.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.