Mike Barnett - Getting started with Code Contracts in Visual Studio 2008

Sign in to queue

Description

Mike Barnett gives a step-by-step tutorial on Code Contracts, a new way to express preconditions, postconditions and invariants in any .NET language. The contracts team have built a runtime instrumentation tool and also advanced static checkers that can understand and reason about contracts.

The Code Contracts library can be downloaded for Visual Studio 2008 from the DevLabs web site. They will be part of the .Net framework 4.0!!!

The Research in Software Engineering team (RiSE) coordinates Microsoft's research in Software Engineering in Redmond, USA.

Embed

Download

Download this episode

The Discussion

  • User profile image
    JoshRoss
    Why put the contract into the method, as opposed to an attribute? How does this code rewriting work with the standard compiler?
  • User profile image
    wil2300
    I watched the Spec# Expert to Expert vid with Erik Meijer a while back and it is pretty interesting stuff. Looking forward to this to see how far it has come.
  • User profile image
    hillr
    My guess is because any expressions (a == b || c != d) you put in an attribute (as a string) can't be verified by the compiler.   Any errors wouldn't be noticed until runtime.
  • User profile image
    mbarnett

    There are several reasons to put the contracts into the code as opposed to using custom attributes. hillr's reply is definitely one of the big reasons. You can see our comments on this topic in our FAQ: http://msdn.microsoft.com/en-us/devlabs/dd492004.aspx.

  • User profile image
    Don Hansen
    I had one question about how to use the contract, and please let me know if this is not the right place to ask.
    Question I have is can the preconditions only go in the constructor?  It makes sense to me that this would be the case, but I figued I'd ask just in case.  I have a custom attribute where I need to check if a variable object inplements a specific interface, but the object is passed in a method and not in the constructor.

    Thanks,
    DoN
  • User profile image
    mbarnett
    This is a fine place to ask, but in general we will be monitoring the forum more, so please use that for questions. (http://social.msdn.microsoft.com/Forums/en-US/codecontracts/threads/) But yes, you can have preconditions on any method, not just constructors. In the video, we just ran out of time before we could write a method more complicated than ToInt() and since that one had no parameters, it didn't make any sense to put a precondition on it.

    Thanks!
  • User profile image
    vesuvius
    I was really excited about contracts and invariants after watching the Spec# video a while ago. It is an absolute delight to find out that this will become part of the base class library, and I don't need to placate managers or developers to use contracts. Just a call to System.Diagnostics.Contracts will do the trick.

    Thank you for the demonstration, I have put aside all my free time to try this out.
  • User profile image
    Chris Richner
    Seems that I can soon throw my custom made post condition check snippets away!! Nice
  • User profile image
    aL_
    woah... that awsome :O
    the static check in perticular is really cool Smiley

    is there a way to customise the runtime checks a little more though? [i just watched the video so this might be obvious in practice]
  • User profile image
    ChazB
    Awesome to say the least!!

    Ensuring that your class is in a correct state would extremely cut back on how often you have to validate your code.

    I'm going to start using this right away.

    Does anyone know if I can use this with Windows Mobile development in Visual Studio?
  • User profile image
    aL_
    aaaaw its only for vs team suite Sad pleeeeese make a version for those of us not able to convice our managers to get TS? Smiley

    --edit--
    oh wait, there is one for pro as well Big Smile
    (use the second link, not the devlabs one)

Add Your 2 Cents