Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design by contract and assert statements

I am interested in the Design by Contract approach. It seems that for preconditions checked exceptions must be used to enforce them.
But for post-conditions and class-invariants I think that assertions are preferred.
Am I right? If I am correct, why for post-conditions and class-invariants assertions which may be disabled are allowed? Shouldn't post-conditions and invariants also be enforced?

like image 720
Cratylus Avatar asked Dec 25 '12 18:12

Cratylus


1 Answers

Post conditions and class invariants on a component can only fail if the component itself is written incorrectly. Unit tests should catch all of these. It's permissible, of course, to actually check them in production, but this isn't necessarily worth the performance tradeoff.

On the other hand, preconditions can fail if the users of that component are incorrect. Tests on the component itself cannot check these, so it's necessary to fail more actively so that those unit tests fail.

like image 163
Louis Wasserman Avatar answered Sep 29 '22 04:09

Louis Wasserman