Archive for October 2010

TDD: The Starwars Answer vs The MacGyver Principle

“But what if you write a bug – like a typo or something – into the test? I mean, your tests aren’t going to be perfect, are they?”

This is a genuine question someone – understandably – asked me earlier this week when I was introducing them to the idea of TDD.

I have two answers to this question: The math answer and the Starwars answer.

1) The math answer:

[note]If P(Error) is the probability of any line of code you write containing an error, and you write t lines of test code and tn lines of production code, where n is greater than 1, then bugs in both tests and production code can only slip through when they coincide, and you will fix tn bugs in production code for every t bugs in the tests.

Don’t dehydrate your code

DRY (don’t repeat yourself) is one of the first principles you learn as a fledgling coder.

When you build your first loop, you’re embracing DRY, and all the wonderful code-shrinking that comes with it. And it feels good, and we’re taught to seek out repetition as a terrible code smell, and do away with it as soon as possible.

But it’s possible to be too DRY
- no, really, it is.