On Fri, Oct 18, 2002 at 12:16:30AM +0900, Paul Brannan wrote:
> The argument I see over and over is that unit testing is an acceptable
> and preferable alternative to contracts.  I'm not sure whether I agree.

[Not strictly related to your post, Paul, just blatantly taking some
words as an excuse to go off on a tangent.]

When building contracts, interfaces and all that into a class, it
seems to me that the programmer is trying to carry out two related,
but different tasks: making the object behave in a certain way, and
certifying that indeed it does.

Now some things bug me about that.  First, as it has already been
discussed in this group, even if a class implements, say, an interface
that includes openFile(String path), inferring that upon calling a
file will be opened is still a matter of deduction and trust.  The
only thing we know for sure is that there is a method called
`openFile' and that it accepts a String argument.

Second, is certifying a behaviour really a class's responsibility?

(I imagine posing that question in a Java group and being flamed to
death.  In fact, I don't think I would ever have thought about that if
I hadn't got a bit of experience in Ruby.)

Think about this: you're an object from some programming language,
you've just got back home after a long day at school where you've
learnt lots of things, when someone rings at your door.  You go open
it and here's a guy asking, ``Can you please do X for me?''

There are three things you could answer:

``Yes.''  (Who taught you said that you can do X, so it must be
true... really?)

``No.'' (Who taught you said that you can't do X, so it must be
true... really?)

``Let's try.''  (They just taught you things.  It's up to who asked
you, to judge if what you can do is enough.)

Now what scenario do you like the best?

*Personally* I mostly like the third.  I'm asking something of
someone, so I'd better ensure that he's up to it.  After that, I trust
him and I don't keep checking over and over.

Could it be that I haven't tested him enough?  Sure...

Is testing expensive, compared to just trusting his certifications?
Sure...

Will I still keep testing before trusting?  Guess it. :-)


Massimiliano