Stroustrup objects to adding Contracts to C++
Paul Backus
snarwin at gmail.com
Tue Nov 10 15:38:47 UTC 2026
On Monday, 9 November 2026 at 18:40:01 UTC, Meta wrote:
> On Monday, 9 November 2026 at 12:19:30 UTC, Paul Backus wrote:
>> Honestly, contracts in D are kind of a flop. Not because
>> there's anything wrong with them in principle, but because the
>> goals you'd use them to achieve are usually achieved more
>> effectively by other means (unit testing, encoding invariants
>> in the type system).
>
> I don't agree. I think they're extremely useful for enforcing
> runtime invariants that are very difficult or impossible to
> express in the type system. You can use unit tests to achieve
> something similar, but its clumsy and they're not really suited
> for what contracts do.
The biggest issue with contracts compared to unit tests is that
unit test failures happen at build/CI time, whereas contract
failures happen at runtime. So even if you are using contracts to
check invariants, you're still going to want unit tests to
actually exercise them. And if you're already writing those tests
anyway, the value added by the contracts is pretty minimal.
Of course you can make argument for them as a defense-in-depth,
belt-and-suspenders kind of tool. But I don't think that argument
alone would justify adding them to a language where they didn't
already exist.
More information about the Digitalmars-d
mailing list