By Clarence Bakirtzidis (firstname.lastname@example.org) for Melbourne Patterns Group on 02/12/2009.
What is Code Contracts API? (From Microsoft Research)"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. The contracts are used to improve testing via runtime checking, enable static contract verification, and documentation generation." Library consists of a set of static methods in the System.Diagnostics.Contract namespaceThe use of a library has the advantage that all .NET languages can immediately take advantage of contracts immediately.Spec# is a Microsoft Research project which extends the C# language with constructs for non-null types, preconditions, postconditions, and object invariants.It is a more advanced research project than Code ContractsAcademic license version can be obtained from Microsoft Research siteCommercial license version can be obtained from DevLabs websiteStandard Edition (no static type checker) – any edition for Visual Studio except Express EditionIncludes the stand-alone contract library, the binary rewriter (for runtime checking), the reference assembly generator, and a set of reference assemblies for the .NET Framework.VSTS Edition (includes static type checker) – Only for Visual Studio Team SystemSame as Standard Edition but also includes the Static checkerTools include:ccrewrite, for generating runtime checking from the contractscccheck, a static checker that verifies contracts at compile-time.ccdoc, a tool that adds contracts to the XML documentation files and to Sandcastle-generated MSDN-style help files.Plan is to add further tools. There is a prototype for a VS 2010 add-in so that inherited contracts show up as you type.Integration includes properties tab called “Code Contracts” for VS projects (can enable/disable various features)
Eiffel was created by Bertrand Meyer in 1985/86Pure object-oriented languageMajor feature is Design-by-contractSupports preconditions, postconditions, invariants, loop invariants, loop variants (ensure loop will end), check (like C assert)Can choose to enable only preconditions (e.g. for 3pp code) to reduce contract checking"void safety" - void is null in Eiffel. e.g. x.f where x is null the compiler can detect if this will succeed at runtimePrecondition - is a condition or predicate that must always be true just prior to the execution of some section of codePostcondtion - is a condition or predicate that must always be true just after the execution of some section of codeInvariant - invariants constrain the state stored in the object. Methods of the class should preserve the invariant. Class invariants are established during construction and constantly maintained between calls to public methods. Temporary breaking of class invariance between private method calls is possible, although not encouraged.
Code Contracts API In .Net
Using the Code Contracts API for design-by-contract in .NET<br />Melbourne Patterns Group Presentation<br />By Clarence Bakirtzidis(email@example.com)<br />
Overview<br />What is the Code Contracts API?<br />Library with Static Methods for DbC<br />It is a spin-off from the learnings of the Spec# project<br />Obtaining and Installing<br />Academic and Commercial Licenses<br />Tools Provided<br />Integration with Visual Studio<br />3<br />
Overview<br />What is Design-by-Contract?<br />Originated from Eiffel<br />Precondition<br />Condition that is checked on entry to method<br />Postcondition<br />Condition that is checked on exit of method<br />Invariant (constrain state of objects)<br />Condition checked on exit of constructor and all public methods and properties<br />Static (Compile-time) and Runtime checking<br />4<br />
Overview<br />Benefits of Code Contracts<br />Runtime Checking and Improved Testability<br />Static Verification<br />API Documentation<br />Examples<br />5<br />
Using Code Contracts<br />.NET 4.0<br />Part of the Base Class Library (BCL)<br />No additional references required<br />.NET 3.5 or earlier<br />Separate assembly Microsoft.Contracts.dll<br />Need to add reference to assembly in your projects<br />Contract classes contains in:<br />System.Diagnostics.Contracts namespace<br />Code Snippetsfor Visual Studio<br />e.g. ci [TAB][TAB] => Contract.Requires(…)<br />6<br />
Using Code Contracts<br />Visual Studio Project Properties<br /><ul><li>Runtime Checking
Main Features<br />Design-by-Contract<br />Runtime Checking<br />Static Checking<br />Contract Inheritance<br />Supports Abstract Methods and Interfaces<br />Generate API documentation<br />Hooks into XML documentation and inserts contract requirements (requires, ensures)<br />8<br />
Runtime Contract Checking<br />Configuration options<br />Level of checking<br />Only public surface contracts<br />Call-site requires<br />Custom rewriter methods<br />Assembly rewriting via ccrewrite.exe<br />Inserts code in project assemblies to enforce contracts at runtime<br />9<br />
Summary<br />Code Contracts bring DbC to the .NET framework<br />Provides static and runtime checking of:<br />Preconditions<br />Postconditions<br />Object invariants<br />Extends generated XML documentation<br />Some known issues (still work in progress)<br />Build slowdown - will be addressed in future<br />Closures - static checking does not work for closures<br />Edit-Continue does not work with code contract rewriting on<br />No contracts allowed on delegates<br />No contracts on iterators that use “yield” as IL the code changed into different form (workaround exists for this)<br />13<br />