Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncle Bob's TDD Rules

Tags:

tdd

UncleBobsThreeTDDRules

  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.

Can someone tell me the difference between 1 and 3? it's not very clear to me.

To me 1 and 3 can be combined or are these rules suggesting order as well?

like image 833
jakstack Avatar asked Feb 25 '14 15:02

jakstack


1 Answers

First of all: I'd take these rules with a grain of salt.

That said, rule one and rule three have a slightly different notion:

Rule 1: You should not write any code without having a failing test.

Rule 3: You should not implement a complete algorithm (even though it would make the test pass) but only the simplest possible (some might say naive) solution to make the test pass.

An example:

Given you want a method that takes a number and returns the very same number. Say you have the following test:

public void Entering1Returns1() {
    assert.That(calculate(1) == 1);
}

This implementation would comply to both rules:

public void calculate(int input) {
    return 1;
}

This one would violate rule 3 (strictly speaking) because it does more than needed:

public void calculate(int input) {
    return input;
}
like image 76
Dennis Traub Avatar answered Nov 03 '22 17:11

Dennis Traub