lyntale: ms code contracts
DESCRIPTION
Lyntale fra Computasdagen 05.05.11TRANSCRIPT
![Page 1: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/1.jpg)
ms code contracts
eih
![Page 2: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/2.jpg)
what is
a code contract?
![Page 3: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/3.jpg)
caller
![Page 4: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/4.jpg)
callee
![Page 5: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/5.jpg)
contract
![Page 6: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/6.jpg)
bool Equals(object o)
a weak contract
![Page 7: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/7.jpg)
what is
design by contract?
![Page 8: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/8.jpg)
“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](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/9.jpg)
invented by
Bertrand Meyer
![Page 10: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/10.jpg)
what does it expect?what does it guarantee?what does it maintain?
a better contract
![Page 11: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/11.jpg)
> prerequisites> postconditions> invariants
dbc tenets
![Page 12: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/12.jpg)
example
stack
![Page 13: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/13.jpg)
> Push(T t)> T Pop()> T Top()> int Count> IsEmpty
stack
![Page 14: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/14.jpg)
dbc by hand
![Page 15: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/15.jpg)
T Pop(){ return _list.RemoveLast();}
example
![Page 16: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/16.jpg)
precondition
T Pop(){ Debug.Assert(!IsEmpty); return _list.RemoveLast();}
![Page 17: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/17.jpg)
void Push(T t){ _list.Add(t);}
example
![Page 18: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/18.jpg)
postcondition
void Push(T t){ try { _list.Add(t); } finally { Debug.Assert(!IsEmpty); }}
![Page 19: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/19.jpg)
invariant
Count >= 0
![Page 20: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/20.jpg)
limitations
tedious!
![Page 21: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/21.jpg)
limitations
clutters the code!
![Page 22: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/22.jpg)
what is
ms code contracts?
![Page 23: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/23.jpg)
dbc.net
![Page 24: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/24.jpg)
code contracts
> rewriter> verifier
![Page 25: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/25.jpg)
rewriter
injects runtime checks
![Page 26: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/26.jpg)
T Pop(){ return _list.RemoveLast();}
example
![Page 27: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/27.jpg)
precondition
T Pop(){ Contract.Requires(!IsEmpty); return _list.RemoveLast();}
![Page 28: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/28.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/29.jpg)
void Push(T t){ _list.Add(t);}
example
![Page 30: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/30.jpg)
postcondition
void Push(T t){ Contract.Ensures(!IsEmpty); _list.Add(t);}
![Page 31: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/31.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/32.jpg)
invariant
[ContractInvariantMethod]private void Invariant(){ Contract.Invariant(Count >= 0);}
![Page 33: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/33.jpg)
verifier
performs static checks
![Page 34: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/34.jpg)
verifier
vs >= premium
![Page 35: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/35.jpg)
verifier
![Page 36: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/36.jpg)
so far
so good
![Page 37: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/37.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/38.jpg)
critique
> ugly syntax> in method body> interface hack
![Page 39: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/39.jpg)
yuck.
![Page 40: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/40.jpg)
what is
spec#?
![Page 41: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/41.jpg)
precondition
T Pop() requires !IsEmpty;{ return _list.RemoveLast();}
![Page 42: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/42.jpg)
postcondition
void Push(T t) ensures !IsEmpty;{ _list.Add(t);}
![Page 43: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/43.jpg)
T Pop() requires !IsEmpty; ensures Count > old(Count); ensures result == old(Top()); { return _list.RemoveLast();}
a stricter contract
![Page 44: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/44.jpg)
much better!
![Page 45: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/45.jpg)
syntax helps
lesson
![Page 46: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/46.jpg)
conclusion
![Page 47: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/47.jpg)
design by contract?
yay!
![Page 48: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/48.jpg)
ms code contracts?
meh.
![Page 49: Lyntale: MS Code Contracts](https://reader036.vdocument.in/reader036/viewer/2022070303/54b7306c4a7959193b8b4613/html5/thumbnails/49.jpg)
spec#?
yay!