Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Code Contracts API In .NET

Code Contracts API In .NET

Slide deck for my presentation on "Code Contracts API In .NET" in the now defunct Melbourne Patterns Group back in 2009.

Clarence Bakirtzidis

December 14, 2009
Tweet

More Decks by Clarence Bakirtzidis

Other Decks in Programming

Transcript

  1. Agenda ž  Overview ž  Using Code Contracts ž  Main Features

    ž  Runtime Contract Checking ž  Static Contract Checking ž  Demo(s) ž  API Reference ž  Summary ž  Resources ž  Questions * DbC = Design-by-Contract 2
  2. Overview ž  What is the Code Contracts API? —  Library

    with Static Methods for DbC —  It is a spin-off from the learnings of the Spec# project ž  Obtaining and Installing —  Academic and Commercial Licenses ž  Tools Provided ž  Integration with Visual Studio 3
  3. Overview ž  What is Design-by-Contract? —  Originated from Eiffel — 

    Precondition ◦  Condition that is checked on entry to method —  Postcondition ◦  Condition that is checked on exit of method —  Invariant (constrain state of objects) ◦  Condition checked on exit of constructor and all public methods and properties —  Static (Compile-time) and Runtime checking 4
  4. Overview ž  Benefits of Code Contracts —  Runtime Checking and

    Improved Testability —  Static Verification —  API Documentation ◦  Examples 5
  5. Using Code Contracts ž  .NET 4.0 —  Part of the

    Base Class Library (BCL) —  No additional references required ž  .NET 3.5 or earlier —  Separate assembly Microsoft.Contracts.dll —  Need to add reference to assembly in your projects ž  Contract classes contains in: —  System.Diagnostics.Contracts namespace ž  Code Snippets for Visual Studio —  e.g. ci [TAB][TAB] => Contract.Requires(…) 6
  6. Using Code Contracts Project Properties Visual Studio Project Properties • 

    Runtime Checking •  Static Checking •  Contract Reference Assembly 7
  7. Main Features ž  Design-by-Contract —  Runtime Checking —  Static Checking

    ž  Contract Inheritance —  Supports Abstract Methods and Interfaces ž  Generate API documentation —  Hooks into XML documentation and inserts contract requirements (requires, ensures) 8
  8. Runtime Contract Checking ž  Configuration options —  Level of checking

    —  Only public surface contracts —  Call-site requires —  Custom rewriter methods ž  Assembly rewriting via ccrewrite.exe —  Inserts code in project assemblies to enforce contracts at runtime 9
  9. Static Contract Checking ž  Configuration options —  Null values — 

    Array bounds —  Arithmetic (div-by-zero) ž  Baseline —  Suppress existing warnings in code base 10
  10. API Reference ž  Preconditions —  Contract.Requires(…) —  EndContractBlock (“legacy- requires”)

    ž  Postconditions —  Contract.Ensures(…) —  Contract.Ensures<E>(…) —  Contract.EnsuresOnThrow<E>(…) ž  Prestate Values —  Contract.Result<T>() —  Contract.OldValue<T>(…) ž  Out Parameters —  Contract.ValueAtReturn<T>(…) ž  Invariants —  Contract.Invariant(…) ž  Attributes —  Pure —  ContractInvariantMethod —  ContractVerification —  ContractPublicPropertyName ž  Quantifiers —  Contract.ForAll —  Contract.Exists ž  Interfaces and Abstract Methods —  ContractClass —  ContractClassFor ž  Other —  Contract.Assert(…) —  Contract.Assume(…) —  ContractException 12
  11. Summary ž  Code Contracts bring DbC to the .NET framework

    ž  Provides static and runtime checking of: —  Preconditions —  Postconditions —  Object invariants ž  Extends generated XML documentation ž  Some known issues (still work in progress) —  Build slowdown - will be addressed in future —  Closures - static checking does not work for closures —  Edit-Continue does not work with code contract rewriting on —  No contracts allowed on delegates —  No contracts on iterators that use “yield” as IL the code changed into different form (workaround exists for this) 13
  12. Resources ž  Code Contracts Project —  http://research.microsoft.com/en-us/projects/ contracts/ ž  MSDN

    Forum —  http://social.msdn.microsoft.com/Forums/en- US/codecontracts/threads ž  Spec# Project —  http://research.microsoft.com/en-us/projects/ specsharp 15