javablogspot

Just another WordPress.com weblog

Posts Tagged ‘TDD’

Contracts? Test-driven? Insight!

Posted by damuchinni on March 14, 2009

The other day I pair-programmed with a new guy at work. He showed my a class and its tests he and another guy had written a few week earlier. I don’t recall exactly what the class did, but it was quite simple, hence the tests was short and simple. Overall well written tests if you ask me.

As we looked at the tests we had the following conversation, which afterwards gave me an new insight to an idea I’ve had a long while: tests are contracts.
He: Most of these tests are for testing that the class logs correctly…
Me: Yes, is that a problem?
He: Well, I know TDD says that you need to write a failing test before you’re allowed to write any production code. But isn’t testing logging a bit over-kill?
Me: I understand what you mean. What kind of logging is this? Why does the class need to log?
He: We needed it to understand the code. For debugging.
Me: Is the logging needed now? Is there some script that parses these logs, for example?
He: No, it’s not needed any more.
Me: In that case I’d say that these tests isn’t needed.

I could go even further and say that those tests shouldn’t be written at all and should be removed. I actually think that tests like these are more confusing then anything else. I’m not saying those two guys who wrote these tests did anything wrong; they were doing TDD and was doing TDD right. What I’m saying is that, in my opinion, TDD isn’t ideal.

Yeah, I hear you’re cries: What?! Heresy! Calm down. I’ll try to explain.

My opinion is that a class’ tests should define what the class has to fulfill to be considered correct. To be precise, with ‘correct’ I mean ‘what makes all things that depend on the class behave correctly’. (I realize that this is an recursive definition of ‘correct’, but you’re a human being so you can handle it. :))

Recall that my pair-programming partner said that the logging wasn’t needed any more. This means that we could remove the part of the code that logs and the class would still be correct according to the definition above. However, the class would not pass its tests because of the tests that tests the logging. This means that the class is over-specified. This is bad. The solution? Ditch the logging tests!

And so my fellow pair-programmer did.

I often say that test-methods should be named shouldFoo since it makes you focus on the behavior that is tested instead of the part that is tested (the tested method for example). I’m thinking of extending this tip to nameing test-methods as shouldFooBecauseBar. If this convention was followed, the the test-methods that tests the logging whould be named shouldLogBecauseWeNeedItforDebugging. That name sound a bit silly, doesn’t it? That because it is silly to test it!

As I said, a class’ tests defines what the class has to fulfill to be correct. In other words, the tests is the contract that the class must fulfill. Having tests that define contracts is much better, I think, than having tests for every little piece of functionality a class have (i.e., TDD). One reason is that it makes it easier to understand how you can change the class without breaking anything.

Now don’t get me wrong, TDD is great, really great. But is it perfect? Of course not, that would be very naive to think (not to mention boring: Nothing more to do here, we’ve find the ideal solution! Now, let’s drink tea all day!).

Is contracts ideal? Probably not. It contracts better? Yes, I think so.

Advertisements

Posted in Test Driven Development | Tagged: , | Leave a Comment »