Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code Contracts at runtime

As far as I read in a nutshell book, code contracts could degrade the runtime performance.

Is it possible to disable code contracts in production?

like image 743
DarthVader Avatar asked Jul 14 '11 18:07

DarthVader


People also ask

What are code contracts?

Code Contracts provide a language-agnostic way to express coding assumptions in . NET programs. The contracts take the form of preconditions, postconditions, and object invariants. Contracts act as checked documentation of your external and internal APIs.

Which of the following methods is used in pre conditions and post conditions for code contracts in net?

NET 4.0. Code Contracts API includes classes for static and runtime checks of code and allows you to define preconditions, postconditions, and invariants within a method.

What is Design by Contract in software engineering?

Design By Contract (DbC) is a software correctness methodology. It uses preconditions and postconditions to document (or programmatically assert) the change in state caused by a piece of a program. Design by Contract is a trademarked term of BertrandMeyer and implemented in his EiffelLanguage as assertions.


2 Answers

The user manual explains this in a fair amount of detail - there are all kinds of options you can have. Each build configuration can have different settings for which contracts are checked at execution time, and it's not an "all or nothing" choice - you can enforce all, some or none of the contracts, based on settings which can be tweaked in Visual Studio.

like image 170
Jon Skeet Avatar answered Oct 06 '22 00:10

Jon Skeet


I have my favorite options described on my blog.

To summarize:

  • In Release mode, I recommend unchecking Perform Runtime Contract Checking but selecting to Build the Contract Reference Assembly. This will place Preconditions in a separate dll which your clients can optionally use (if they check Call-site Requires Checking), but removes all overhead if they don't check that option.
  • In Debug mode, set Perform Runtime Contract Checking to Full.

Some people prefer Preconditions to be included in their Release build. This is particularly useful if distributing via NuGet because they don't support Code Contract dlls. For my NuGet packages, I'm migrating towards including Preconditions in the Release builds, but also having a separate download for a "Release without Preconditions" build.

like image 43
Stephen Cleary Avatar answered Oct 06 '22 00:10

Stephen Cleary