+ All Categories
Home > Technology > Lyntale: MS Code Contracts

Lyntale: MS Code Contracts

Date post: 15-Jan-2015
Category:
Upload: einar-host
View: 859 times
Download: 2 times
Share this document with a friend
Description:
Lyntale fra Computasdagen 05.05.11
Popular Tags:
49
ms code contracts eih
Transcript
Page 1: Lyntale: MS Code Contracts

ms code contracts

eih

Page 2: Lyntale: MS Code Contracts

what is

a code contract?

Page 3: Lyntale: MS Code Contracts

caller

Page 4: Lyntale: MS Code Contracts

callee

Page 5: Lyntale: MS Code Contracts

contract

Page 6: Lyntale: MS Code Contracts

bool Equals(object o)

a weak contract

Page 7: Lyntale: MS Code Contracts

what is

design by contract?

Page 8: Lyntale: MS Code Contracts

“Unless design by contract evokes images of curly hair and a French landmark in your head, you got it wrong.”

Page 9: Lyntale: MS Code Contracts

invented by

Bertrand Meyer

Page 10: Lyntale: MS Code Contracts

what does it expect?what does it guarantee?what does it maintain?

a better contract

Page 11: Lyntale: MS Code Contracts

> prerequisites> postconditions> invariants

dbc tenets

Page 12: Lyntale: MS Code Contracts

example

stack

Page 13: Lyntale: MS Code Contracts

> Push(T t)> T Pop()> T Top()> int Count> IsEmpty

stack

Page 14: Lyntale: MS Code Contracts

dbc by hand

Page 15: Lyntale: MS Code Contracts

T Pop(){ return _list.RemoveLast();}

example

Page 16: Lyntale: MS Code Contracts

precondition

T Pop(){ Debug.Assert(!IsEmpty); return _list.RemoveLast();}

Page 17: Lyntale: MS Code Contracts

void Push(T t){ _list.Add(t);}

example

Page 18: Lyntale: MS Code Contracts

postcondition

void Push(T t){ try { _list.Add(t); } finally { Debug.Assert(!IsEmpty); }}

Page 19: Lyntale: MS Code Contracts

invariant

Count >= 0

Page 20: Lyntale: MS Code Contracts

limitations

tedious!

Page 21: Lyntale: MS Code Contracts

limitations

clutters the code!

Page 22: Lyntale: MS Code Contracts

what is

ms code contracts?

Page 23: Lyntale: MS Code Contracts

dbc.net

Page 24: Lyntale: MS Code Contracts

code contracts

> rewriter> verifier

Page 25: Lyntale: MS Code Contracts

rewriter

injects runtime checks

Page 26: Lyntale: MS Code Contracts

T Pop(){ return _list.RemoveLast();}

example

Page 27: Lyntale: MS Code Contracts

precondition

T Pop(){ Contract.Requires(!IsEmpty); return _list.RemoveLast();}

Page 28: Lyntale: MS Code Contracts

rewritten toT Pop(){ if (__ContractsRuntime.insideContractEvaluation <= 4) { try { __ContractsRuntime.insideContractEvaluation++; __ContractsRuntime.Requires(!this.IsEmpty, null, "!IsEmpty"); } finally { __ContractsRuntime.insideContractEvaluation--; } } return this._list.RemoveLast<T>();}

Page 29: Lyntale: MS Code Contracts

void Push(T t){ _list.Add(t);}

example

Page 30: Lyntale: MS Code Contracts

postcondition

void Push(T t){ Contract.Ensures(!IsEmpty); _list.Add(t);}

Page 31: Lyntale: MS Code Contracts

rewritten to

void Push(T t){ this._list.Add(t); if (__ContractsRuntime.insideContractEvaluation <= 4) { try { __ContractsRuntime.insideContractEvaluation++; __ContractsRuntime.Ensures(!this.IsEmpty, null, "!IsEmpty"); } finally { __ContractsRuntime.insideContractEvaluation--; } }}

Page 32: Lyntale: MS Code Contracts

invariant

[ContractInvariantMethod]private void Invariant(){ Contract.Invariant(Count >= 0);}

Page 33: Lyntale: MS Code Contracts

verifier

performs static checks

Page 34: Lyntale: MS Code Contracts

verifier

vs >= premium

Page 35: Lyntale: MS Code Contracts

verifier

Page 36: Lyntale: MS Code Contracts

so far

so good

Page 37: Lyntale: MS Code Contracts

T Pop(){ Contract.Requires(!IsEmpty); Contract.Ensures(Count < Contract.OldValue(Count)); Contract.Ensures(Contract.Result<T>() .Equals(Contract.OldValue(Top()))); return _list.RemoveLast();}

a stricter contract

Page 38: Lyntale: MS Code Contracts

critique

> ugly syntax> in method body> interface hack

Page 39: Lyntale: MS Code Contracts

yuck.

Page 40: Lyntale: MS Code Contracts

what is

spec#?

Page 41: Lyntale: MS Code Contracts

precondition

T Pop() requires !IsEmpty;{ return _list.RemoveLast();}

Page 42: Lyntale: MS Code Contracts

postcondition

void Push(T t) ensures !IsEmpty;{ _list.Add(t);}

Page 43: Lyntale: MS Code Contracts

T Pop() requires !IsEmpty; ensures Count > old(Count); ensures result == old(Top()); { return _list.RemoveLast();}

a stricter contract

Page 44: Lyntale: MS Code Contracts

much better!

Page 45: Lyntale: MS Code Contracts

syntax helps

lesson

Page 46: Lyntale: MS Code Contracts

conclusion

Page 47: Lyntale: MS Code Contracts

design by contract?

yay!

Page 48: Lyntale: MS Code Contracts

ms code contracts?

meh.

Page 49: Lyntale: MS Code Contracts

spec#?

yay!


Recommended