Test driven development (TDD)

TDD objective is getting “clean code that works”(Test Driven Development By Example, Kent Beck).

There are two main rules to TDD.

·         "Write new code only if you have a failing automated test.

·         Eliminate Duplication"

(Test Driven Development By Example, Kent Beck)

Those two rules establish and order for programming:

1.       "Write a little test that doesn’t work". (Test Driven Development By Example, Kent Beck) 

2.       Make whatever it takes to make that test work.

3.       Eliminate repetition created while making the test work.

1)      Write a test that doesn’t work: Write down a to do list. Then in the simplest way possible code one of the “items” in it, without worrying about whether it compiles or not, but thinking how it ought to work. As you do this add tasks to the to do list when you think of them.

Once you’re finished, try compiling. Make the changes in your program in order to make it compile, but always in the simplest way possible. i.e.: If it says you don’t have a certain class, just define it. DON’T do anything extra.

2)      Once your program compiles, run the test. If it fails find the reason to it and make the FEWEST

POSSIBLE CHANGES to make it succeed.

3)      Now your test should succeed. It’s time to get rid of duplication. Start doing it and with every change you make, the test must still succeed. If it does, cross the item from the to do list.

Strategies:

There are three strategies for quickly getting test to succeed:

Ø  "Fake It: return a constant and gradually replace constants with variables" (Test Driven Development By Example, Kent Beck), until reaching the real code.

Ø  "Obvious Implementations: type in the real implementations." (Test Driven Development By Example, Kent Beck)

Ø  Triangulation:  To triangulate you need more than one example to generalize code. Try defining in different situations what kind of variability you’re trying to support and figure out how to do it for more than one situation.

Another important thing about TDD is that should try to avoid writing tests when a previous one is not working. Instead try going back to the reason why the first test isn’t working, and then focus on the next test.