43
Code Contracts Dmytro Mindra RnD Tech Lead Lohika Labs Пятая встреча Microsoft .Net User Group Одесса, 2011

Code contracts by Dmytro Mindra

Embed Size (px)

Citation preview

Page 1: Code contracts by Dmytro Mindra

Code Contracts

Dmytro MindraRnD Tech Lead

Lohika Labs

Пятая встреча Microsoft .Net User Group Одесса, 2011

Page 2: Code contracts by Dmytro Mindra

© Drake Emko & Jen Brodzik, 2001

Page 3: Code contracts by Dmytro Mindra

© Drake Emko & Jen Brodzik, 2001

Page 4: Code contracts by Dmytro Mindra

© Drake Emko & Jen Brodzik, 2001

Page 5: Code contracts by Dmytro Mindra

Design by Contract

Bertrand MeyerDbC authorPublished 11 booksincluding “Object-Oriented

Software Construction”

First edition: 1988Second edition: 1997

Page 6: Code contracts by Dmytro Mindra

Terms

• Client must pay the fee (obligation) and is entitled to get the product (benefit).

• Supplier must provide a certain product (obligation) and is entitled to expect that the client has paid its fee (benefit)

• Contract. Both parties must satisfy certain obligations, such as laws and regulations, applying to all contracts

Page 7: Code contracts by Dmytro Mindra

Terms

Client«interface»Interface Supplier

Page 8: Code contracts by Dmytro Mindra

Contract

• Contract is precise and verifiable interface specifications for software components, which extend the ordinary definition of abstract data types with preconditions, postconditions and invariants. [8]

Page 9: Code contracts by Dmytro Mindra

Contract

• Pre-conditions [9]In computer programming, a precondition is a condition or predicate that must always be true just prior to the execution of some section of code or before an operation in a formal specification.

• Post-conditions [10]In computer programming, a postcondition is a condition or predicate that must always be true just after the execution of some section of code or after an operation in a formal specification.

• Invariants [11]In computer science, a predicate is called an invariant to a sequence of operations provided that: if the predicate is true before starting the sequence, then it is true at the end of the sequence.

Page 10: Code contracts by Dmytro Mindra

Contract verification

• Pre-condition fails– Error in client code

• Post-condition or Invariant fails – Error in supplier code

Page 11: Code contracts by Dmytro Mindra

Other approaches

• IF-THEN-TROWif(condition1)

throw Exception1if (condition2)

throw Exception2

• Debug.Assert

• Drawbacks:– No Inheritance– Inconvenient postconditions

Page 12: Code contracts by Dmytro Mindra

Converting legacycontracts

Converting if-throw

void MyMethod(Foo foo){ if (foo == null) throw new ArgumentNullException(...); Contract.EndContractBlock();

... normal method code ...}

Page 13: Code contracts by Dmytro Mindra

Spec#

class Example { int x;

void Inc(int y) ensures old(x) < x; { x += y; }}

Page 14: Code contracts by Dmytro Mindra

New Generation

• Spec#– Source code rewrite– C# only (superset of C#

v2.0)

• Code Contracts– IL Rewrite– Any language from VB to

C#– Faster

Page 15: Code contracts by Dmytro Mindra

Code Contracts

How to start using Code Contracts ?

Page 16: Code contracts by Dmytro Mindra

Visual Studio 2010

• Declarative contracts are included in .NET 4.0 (System.Diagnostics.Contracts)

• Tools are needed to – generate runtime checking from the

contracts(ccrewrite)– do a static check that verifies contracts at compile-

time (cccheck)– add contracts to the XML documentation files (ccdoc)LOCATION: [Program Files]\Microsoft\Contracts\Bin\

Page 17: Code contracts by Dmytro Mindra

System.Diagnostics.Contracts• Contract• Attributes

– ContractClassAttribute– ContractClassForAttribute– ContractInvariantMethodAttribute– ContractPublicPropertyNameAttribute– ContractReferenceAssemblyAttribute– ContractRuntimeIgnoredAttribute– ContractVerificationAttribute– PureAttribute ( is not enforced by analysis tools )

• ContractFailedEventArgs• ContractFailureKind (enum)

Page 18: Code contracts by Dmytro Mindra

Contract methods

• Pre-conditions: Requires• Post-conditions: Ensures• Invariants: Invariant

See also: EnsuresOnThrow<TException>Requires<TException>

Page 19: Code contracts by Dmytro Mindra

Preconditions in Action public class Customer {

private int _ID; public int ID { get { return _ID; } set { if (value <= 0) throw new ArgumentException(); _ID = value; } }}

public class Customer{ private int _ID; public int ID { get { return _ID; } set {

Contract.Requires(value > 0);

_ID = value; } }}

Page 20: Code contracts by Dmytro Mindra

Demo: Basic + IL Spy

Page 21: Code contracts by Dmytro Mindra

Processing collections

• Integer range– ForAll(Int32, Int32, Predicate<Int32>)– Exists(Int32, Int32, Predicate<Int32>)

• CollectionForAll<T>(IEnumerable<T>, Predicate<T>)Exists<T>(IEnumerable<T>, Predicate<T>)

Page 22: Code contracts by Dmytro Mindra

Demo: Collections

Page 23: Code contracts by Dmytro Mindra

Result processing

• OldValue<T>• Result<T>• ValueAtReturn<T>

Page 24: Code contracts by Dmytro Mindra

Demo: Results

Page 25: Code contracts by Dmytro Mindra

Other

• Assert - Checks for a condition• Assume - Instructs code analysis tools to

assume that the specified condition is true, even if it cannot be statically proven to always be true. Only for static checks. In runtime is treated like Assert. [3]

• EndContractBlock - for legacy contracts

Page 26: Code contracts by Dmytro Mindra

Assert & Assumepublic void Invoke() {

int x = CalculateSomeValues(); // Tell the checker to verify whether// x>0. // (The checker might // be unable to do it.)

Contract.Assert( x>0 );

// Rest of the code }

public void Invoke() {

int x = CalculateSomeValues(); // Explicitly tell the checker that //x>0

Contract.Assume( x>0 );

// Rest of the code }

Page 27: Code contracts by Dmytro Mindra

Inheritance

• Two rules[7]:– When you override a method (or implement an

interface method) you inherit its contracts.– You can't add extra preconditions to inherited

ones, but you can make invariants and postconditions stronger. • E.g was require x>10• Added require x>100• Now x = 20 fulfills 1st require but violates 2nd;

Page 28: Code contracts by Dmytro Mindra

Demo: Inheritance& Pitfalls

Page 29: Code contracts by Dmytro Mindra

ContractFailed Handling

Contract.ContractFailed += ContractContractFailed;

static void ContractContractFailed(object sender, ContractFailedEventArgs

e) { e.SetHandled(); // exception handled

Console.WriteLine(e.Message);}

Page 30: Code contracts by Dmytro Mindra

Demo: ContractFailedfandling

Page 31: Code contracts by Dmytro Mindra

custom contracts &custom rewriters methods

public static class RuntimeFailureMethods {  public static void Requires(bool cond, string userMsg, string condText) { }  public static void Ensures(bool cond, string userMsg, string condText) { }…

}See user manual 7.7. (page 34) [12]

Page 32: Code contracts by Dmytro Mindra

Code snippets

• cr Contract.Requires(...);• ce Contract.Ensures(...);• ci Contract.Invariant(...);

• More in user manual 6.3. (page 26) [12]

Page 33: Code contracts by Dmytro Mindra

Why not validate everything?

• Performance!

Page 34: Code contracts by Dmytro Mindra

Summary and prospects

• Code Contracts are evolving• BCL is driven by Code Contracts• Static checking• Code Contracts may lead to better design• Auto generated documentation• Another tool in your toolbelt

Page 35: Code contracts by Dmytro Mindra

PEXPath-based program exploration

Page 36: Code contracts by Dmytro Mindra

Pex Demo

Page 37: Code contracts by Dmytro Mindra

Additional reading

Page 38: Code contracts by Dmytro Mindra

References[1] Design by Contract - A Conversation with Bertrand Meyer, Part II by Bill Vennershttp://www.artima.com/intv/contracts.html[2] Defensive programminghttp://en.wikipedia.org/wiki/Defensive_programming[3] Dino Esposito, Code Contracts Preview: Preconditionshttp://dotnetslackers.com/articles/net/Code-Contracts-Preview-Preconditions.aspx[4] Dino Esposito, Code Contracts Preview: PostConditionshttp://dotnetslackers.com/articles/net/Code-Contracts-Preview-PostConditions.aspx[5] Dino Esposito, Code Contracts Preview: Invariantshttp://dotnetslackers.com/articles/net/Code-Contracts-Preview-Invariants.aspx[6] Dino Esposito, Code Contracts Preview: Assert & Assumehttp://dotnetslackers.com/articles/net/Code-Contracts-Preview-Assert-Assume.aspx[7] Jon Skeet, Code Contracts in C#http://www.infoq.com/articles/code-contracts-csharp

Page 39: Code contracts by Dmytro Mindra

References[8] Design by Contract - Wikipediahttp://en.wikipedia.org/wiki/Design_by_contract[9] Precondition - Wikipediahttp://en.wikipedia.org/wiki/Precondition[10] Postcondition - Wikipediahttp://en.wikipedia.org/wiki/Postcondition[11] Invariant - Wikipedia http://en.wikipedia.org/wiki/Invariant_(computer_science)[12] Code Contracts User Manualhttp://research.microsoft.com/en-us/projects/contracts/userdoc.pdf[13] Code contracts and inheritancehttp://stefanoricciardi.com/2009/07/17/code-contracts-and-inheritance/[14] Assertions in Managed Codehttp://msdn.microsoft.com/en-us/library/ttcc4x86.aspx

Page 40: Code contracts by Dmytro Mindra

C# 4.0 in a nutshell

Page 508

Page 41: Code contracts by Dmytro Mindra

Object-Oriented Software Construction

Object-Oriented Software ConstructionBertrand Meyer1988,1997

Page 42: Code contracts by Dmytro Mindra

QUESTIONS ?

Page 43: Code contracts by Dmytro Mindra

THANK YOU !