DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
Code Contracts API In .Net
1. Using the Code Contracts API for design-by-contract in .NET Melbourne Patterns Group Presentation By Clarence Bakirtzidis(clarenceb@gmail.com)
2. 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
3. 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
4. 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
5. Overview Benefits of Code Contracts Runtime Checking and Improved Testability Static Verification API Documentation Examples 5
6. 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 Snippetsfor Visual Studio e.g. ci [TAB][TAB] => Contract.Requires(…) 6
10. 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
11. 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
12. Static Contract Checking Configuration options Null values Array bounds Arithmetic (div-by-zero) Baseline Suppress existing warnings in code base 10
15. 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
By Clarence Bakirtzidis (clarenceb@gmail.com) 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.