Code Contracts – my opinion on them anyway…

One of the great things that came out of the PDC for me was the confirmation that code contracts would be coming to .NET 4.0. I was a big fan of Spec# [1] but admittedly I never thought it would make it into the mainstream C# or VB.NET languages even though the prior solution provides the more concise and descriptive syntax. On Sunday I played around with the library released at the PDC [2] and in general it all looks good. For the astute reader you will know that I co-authored a book on Data Structures and Algorithms [3] with Luca [4] not so long ago (we will probably release an update just before Christmas, more on that later) and each algorithm within the book states both pre and post conditions. We did consider explicitly defining invariants as well; however we opted not to for conciseness.

While I’m very much encouraged by the inclusion of code contracts in .NET 4.0 I’m still a little apprehensive about it in API form, maybe it’s because I was set on a language based feature set? I’m just not sure.

The big thing here is that there will be something in .NET 4.0 and hopefully its inclusion in the official distribution will emphasise to developers the importance of specifying explicitly contracts to which their algorithms will adhere to. Another interesting thing that came up in the session on code contracts [5], which coincidentally shared its session with Pex [ 6] was the announcement that there would exist tooling that would analyse the contracts within your code and inject the necessary documentation in there. Whether this will annotate the source code with xml-doc or do something else I’m not sure though. The former was the one most alluded to during the PDC session.

Sorry about not posting recently I’ve just moved to Australia from the UK to do my PhD. Today actually I shared my desk with Gordon Bell [7] who was visiting the Microsoft QUT eResearch Centre [ 8] of which I am now a member.


1. Mike Barnett, K. Rustan M. Leino, and Wolfram Schulte. The Spec# programming system: An overview. CASSIS . 2004, Vol. 3362.

2. Code Contracts download. Microsoft Research. [Online] 2008.

3. DotNetSlackers. [Online] 2008.

4. Tongo, Luca Del. [Online]

5. Channel9. [Online] 2008.

6. Microsoft Research. [Online] 2008.

7. Bell, Gordon. [Online]

8. Microsoft QUT eResearch Centre. [Online]


One thought on “Code Contracts – my opinion on them anyway…”

  1. I agree. I’d definitely like to see it included in pre/post/invariant keywords like in Spec#, but for the time being, this is better than nothing.


Leave a Reply

Your email address will not be published. Required fields are marked *