Hacker News new | ask | show | jobs
by mempko 585 days ago
Wow, people miss the point of contracts. You never want to turn them off in production, because that's where all the weird shit happens that you never thought of. Contracts catch bugs in those times. Maybe people find them useless precisely because they turn them off.
1 comments

Finally, somebody said it! I am always bemused when people say they turn off contracts in production code. They don't understand the difference between contracts (a program state guarantee) and testing for special/error cases (must be handled explicitly). The excuse usually given is runtime performance which can be mitigated by using a subset from pre/post-conditions/invariants.
Exactly. What I do is litter my code with contracts. I then profile to find the hot loop and minimize (not turn them off) there. For the 99% of the other code, the user won't notice any slowdown.

The value is just too high to turn them off in production. Do you want bug free code? This is the only way I know how to do it.

C++ contracts as currently designed can execute 4+ times per call to do full checking. That gets expensive quickly.
But they also give you 4 different evaluation semantics (ignore, observe, enforce, quick_enforce) to tweak as needed. You don't need to do full checking everywhere and always.