[Python-ideas] Simplicity of C (was why is design-by-contracts not widely)

Chris Angelico rosuav at gmail.com
Sun Sep 30 04:33:08 EDT 2018

On Sun, Sep 30, 2018 at 6:03 PM Steven D'Aprano <steve at pearwood.info> wrote:
> On Sun, Sep 30, 2018 at 02:50:28PM +1000, Chris Angelico wrote:
> > And yet all the examples I've seen have just been poor substitutes for
> > unit tests. Can we get some examples that actually do a better job of
> > selling contracts?
> In no particular order...
> (1) Distance

Don't doctests deal with this too? With the exact same downsides?

> (2) Self-documenting code


> (3) The "Have you performed the *right* tests?" problem

Great if your contracts can actually be perfectly defined. Every time
a weird case is mentioned, those advocating contracts (mainly Marko)
give examples showing "hey, contracts can do that too", and they're
just testing specifics.

> (4) Inheritance

Okay, that one I 100% grant you.

> (5) Unit tests and contracts are complementary, not alternatives

That I agree with.

> (6) Separation of concerns: function algorithm versus error checking

Agreed, so long as you can define the contract in a way that isn't
just duplicating the function's own body.

> (7) You can't unit test loop invariants


> (8) Executable documentation

Granted, but there are many forms of that.

Contracts are great for some situations, but I'm seeing a lot of cases
where they're just plain not, yet advocates still say "use contracts,
use contracts". Why?


More information about the Python-ideas mailing list