June 25, 2018
Hot Topics:

Understanding and Benefiting from Code Contracts in .NET 4.0

  • September 4, 2009
  • By Jani Järvinen
  • Send Email »
  • More Articles »

Note that sometimes, if you do a regular build in Visual Studio, you might not see the results in the in the Error List window at all. If this is the case, doing a full rebuild instead of a normal build will reveal the results. Be sure to have the Error List visible all the time, as Visual Studio might not pop the window (panel) up automatically especially if compilation succeeds. This happens with the version 1.2.20518.12 of Code Contracts (the version number is visible in the Code Contracts property sheet in project properties).

Now back to the code. To see how Code Contracts can warn you of problems, assume you would change the calling code to this:

  int avg = samples.Average(new int[] { });

With the empty array, the number of samples is zero, and the contract would be violated. If you now (re)build the code, the results will change to these:

  contracts: requires is false
    + location related to previous warning
  contracts: Checked 1 assertion: 1 false

Although the error message is not very specific, it shows that the given contract is being violated, and the Line column shows the location where this happens in the code (double-clicking the line will however fail to locate the correct source line, but you can browse the location manually). The line "location related to previous warning" shows the location of the contract check, respectively.

Using Code Contracts in real applications

Using Code Contracts in .NET applications has many potential benefits. Although the .NET implementation of Code Contracts is new and hasn't been available for long, the concept of Code Contracts is not new. For instance, it has been used in the Java world for a long time already, and is a native part of certain programming languages such as Eiffel and Oxygene. The basics of Code Contracts were investigated in a Microsoft Research project that created the Spec# language. As such, certain best practices have already emerged on how to use Code Contracts and the so- called contract programming.

Effectively using contracts in applications starts from proper architecture design. Contracts are most efficient when combined with loosely-couple objects, which luckily happens to be a common design principle today. On the other hand, deep dependencies between parts of the application make using contracts more difficult, though not impossible.

Many developers and their companies have started using unit tests to have a certain guarantee of the code quality. Code Contracts can be combined with unit tests, but care should be taken to properly manage contract exceptions, which are by default assertion failures. The biggest problem with assertion failures is that they pop up a message box on the screen, effectively stopping the execution of the application until somebody clicks a button on the message box. For the same reasons, running automated unit tests that use Code Contracts would stop at the first contract violation.

Although Code Contracts is still in early stages of development, there are already good solutions to this problem. The easiest way to stop getting assertion failures is to simply uncheck a box in Code Contracts settings. Under the Runtime Checking group, there is a setting called Assert on Contract Failure. Unchecking (removing the mark) changes assertions to regular exceptions, which can easily be handled by the unit tests in Visual Studio (Figure 5). The other way is to create a custom event handler to process the violation.

Click here for larger image

Figure 5. Converting assertions into regular exceptions.

In addition to tackling the assertion failures within unit tests, you might need to take into consideration the fact that runtime Code Contracts checks are by default disabled. This means that effectively, if you are using unit tests in your code, the contracts might not be checked at all though they exist in the code. Currently, Visual Studio's own unit testing framework has no understanding of Code Contracts, and thus will not notice if they are missing.

Page 3 of 4

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date