design by contract versus doctest

Peter Hansen peter at engcorp.com
Mon Apr 5 15:12:33 EDT 2004


aku wrote:
> A nr of unittests will
> still be necessary to ensure preconditions, but in
> the (oversimplified) example I gave (n = 1), the test
> "if n < 0: ..." isn't necessary anymore and certainly not *in*
> the called function itself.
> 
> The "downside" with DBC I see is: when the caller *cannot*
> ensure a precondition, then the behavior in the called
> function becomes unpredictable. 

I agree that that appears to be a downside.  Furthermore,
if you have to verify in calling code that preconditions
are met, don't you end up with duplication outside of the
function that would otherwise have the check in it?

Well written code will have less duplication, not more,
so I'm still unclear how moving the check out of the
function (from your original example from doctest)
will actually improve things...

And if the code that checks this is in the function,
then a single unit test can verify that the check is
in place and working properly.  It sounds like moving
the check out of that function will lead to more
tests and/or more checks in other places, neither of
which is a good idea.

But I'm certainly no expert in (or proponent of) DBC...

-Peter



More information about the Python-list mailing list