Scott Dorman

ephemeral segment

  Home  |   Contact  |   Syndication    |   Login
  566 Posts | 9 Stories | 568 Comments | 51 Trackbacks

News


Post Categories

Image Galleries



Creative Commons License


Microsoft MVP


MCP Profile


Locations of visitors to this page

Subscribers to this feed

TwitterCounter for @sdorman

View blog authority

Add to Technorati Favorites

Windows Live Alerts

AddThis Social Bookmark Button

LinkedIn profile

Community Credit profile

The Code Project

Follow me on Twitter

Get Free Shots from Snap.com

Community Credit Hall of Fame

Get Feedghost

Xobni outlook add-in for your inbox

Windows Live Translator


Support This Site

Tag Cloud


Article Categories

Archives

Post Categories

Image Galleries

If anyone is familiar with the Microsoft Research (MSR) project Spec# you will be familiar with the idea of contract based programming. CLR 4.0 takes the idea of contract based programming (and more specifically code contracts to ensure code validity) from Spec# and adds it to the CLR. This now means that code contracts are available to any .NET language.

The premise behind code contracts is that you (the programmer) know a lot about your code, including when input arguments should and shouldn’t be null (or other bad inputs) and what the return value properties of a method should be.

Before code contracts there were very few ways to tell the compiler or runtime about this metadata and have it help look for errors. With code contracts in CLR 4.0, you now have this ability.

public void BuyMoreStuff(Item[] cart, ref Decimal totalCost, Item i)
{       
    CodeContract.Requires(totalCost >=0);
    CodeContract.Requires(cart != null);
    CodeContract.Requires(CodeContract.ForAll(cart, s => s != i));

    CodeContract.Ensures(CodeContract.Exists(cart, s => s == i);
    CodeContract.Ensures(totalCost >= CodeContract.OldValue(totalCost));
    CodeContract.EnsuresOnThrow<IOException>(totalCost == CodeContract.OldValue(totalCost));

    // Do some stuff
    …
}

As you can see, there is a new static class CodeContract that makes the contracts available. This allows you to go way beyond simple Assert statements and will actually allow you to (almost) declaratively specify certain rules about your method inputs and outputs.

Code contracts are split into two different categories: pre-conditions (which are very much like Asserts) and post-conditions. The pre-conditions (like Requires) are actually translated by the compiler to the correct code for you while the post-conditions are handled by a postprocessor (essentially an IL rewriter) to insert the appropriate code before each return statement in the function.

It’s probably a safe bet that C# 5.0 will include some simplified syntax sugar to make using contracts even easier, and possibly some attributes to handle this as well.

Technorati Tags: , ,
Digg This
posted on Friday, November 07, 2008 2:58 PM

Feedback

# re: CLR 4.0: Code Contracts 11/7/2008 4:53 PM Colin Angus Mackay
Definitely something to look forward to.


# re: CLR 4.0: Code Contracts 11/15/2008 10:15 PM Scott
@ColinMackay, yes this is definately something to look forward to.

@James, dynamic lookup is certainly interesting and really brings a lot of possiblities.

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: