Transcript
Page 1: Microsoft Code Contracts How to program Pre-conditions, Post-conditions, and Object Invariants Microsoft Code Contracts1

Microsoft Code Contracts 1

Microsoft Code Contracts

How to program Pre-conditions, Post-conditions, and Object Invariants

Page 2: Microsoft Code Contracts How to program Pre-conditions, Post-conditions, and Object Invariants Microsoft Code Contracts1

Microsoft Code Contracts 2

Specifying Object Invariants• Annotate a method [ContractInvariantMethod]• Inside the method you specify the object invariants

[ContractInvariantMethod] private void ObjectInvariant() { Contract.Invariant(Salary >= 0); // Salary is a property Contract.Invariant(!String.IsNullOrEmpty(Name)); }

• If you write “hard” object invariants you may have to make an explicit constructor to initialize properties to legal values• In case the default values are not legal

Page 3: Microsoft Code Contracts How to program Pre-conditions, Post-conditions, and Object Invariants Microsoft Code Contracts1

Microsoft Code Contracts 3

The class ContractSpecifying pre- and post-conditions• Namespace System.Diagnostics.Contracts

• http://msdn.microsoft.com/en-us/library/system.diagnostics.contracts.contract(v=vs.110).aspx

• Some interesting static methods• Requires(Boolean expression)

• Used to specify pre-condition• Comes in 4 variations

• With / without message• With / without specific exception

• The default exception is ContractException. This exception is not public, so you cannot catch it!• Normal exceptions are handy when you Unit test

• Ensures(Boolean expression)• Used to specify post-condition• Comes in 4 variations … (like Requires)

• EnsuresOnThrow<Texception>(…)

Page 4: Microsoft Code Contracts How to program Pre-conditions, Post-conditions, and Object Invariants Microsoft Code Contracts1

Microsoft Code Contracts 4

Static vs. run-time analysis• Static analysis• Conditions and invariants are checked at compile-time• Violations shown in Visual Studio (underlining)

• Run-time analysis• Conditions and invariants are checked at run-time.• Violations reported as messages or exceptions

Page 5: Microsoft Code Contracts How to program Pre-conditions, Post-conditions, and Object Invariants Microsoft Code Contracts1

Microsoft Code Contracts 5

Configuration: Plugin needed• Code Contracts can be

configured in many ways• To do this you need a

plugin for Visual Studio• Code Contracts for .NET

• http://visualstudiogallery.msdn.microsoft.com/1ec7db13-3363-46c9-851f-1ce455f66970

• In Visual studio right click a project and you’ll see an extra menu item “Code Contracts”

Page 6: Microsoft Code Contracts How to program Pre-conditions, Post-conditions, and Object Invariants Microsoft Code Contracts1

Microsoft Code Contracts 6

References and further readings• Nagel et al. Professional C# 5.0 and .NET 4.5.1, Wrox 2014

• Code Contracts, page 522-528

• Microsoft Research: Code Contracts• http://research.microsoft.com/en-us/projects/contracts/

• MSDN Code Contracts• http://msdn.microsoft.com/en-us/library/dd264808(v=vs.110).aspx

• MSDN Contract Class• http://msdn.microsoft.com/en-us/library/system.diagnostics.contracts.contract(v=vs.110).asp

x

• Code Contracts for .NET• Plugin for Visual Studio• http://visualstudiogallery.msdn.microsoft.com/1ec7db13-3363-46c9-851f-1ce455f66970


Top Related