Hacker News new | ask | show | jobs
by rramadass 585 days ago
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.
2 comments

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.