Hacker News new | ask | show | jobs
by int_19h 584 days ago
I find it a very dubious assertion particularly with respect to performance. Well-written idiomatic code in most languages should validate inputs anyway; it's just done in an ad-hoc way - stuff like ArgumentException in .NET or ValueError etc in Python, for example. Contracts are just a way to formalize it, not only for the benefit of static analyzers, but also for better integration with existing tooling (generated docs etc).

And no, you don't have to write a code contract for every single function. Code contracts are most useful in the same exact place you'd do other kinds of contracts, like say documenting the public API of a library. From there it's diminishing gains to extend them deeper into the implementation, but in any case, one can always find a balance between performance and enforced contract checks. Not all code needs to be blazing fast, and erring on the side of correctness is preferable.