SlideShare a Scribd company logo
ms code contractseih
what isa code contract?
caller
callee
contract
a weakcontractbool Equals(object o)
what isdesign by contract?
“Unless design by contract evokes images of curly hair and a French landmark in your head, you got it wrong.”
invented by Bertrand Meyer
a better contractwhat does it expect?what does it guarantee?what does it maintain?
dbc tenets> prerequisites> postconditions> invariants
examplestack
stack> Push(Tt)> T Pop()> T Top()> int Count> IsEmpty
dbc by hand
exampleT Pop(){  return _list.RemoveLast();}
preconditionT Pop(){Debug.Assert(!IsEmpty);  return _list.RemoveLast();}
examplevoidPush(T t){  _list.Add(t);}
postconditionvoidPush(T t){  try {    _list.Add(t);}  finally {    Debug.Assert(!IsEmpty);    }}
invariantCount >= 0
limitationstedious!
limitationsclutters the code!
what isms code contracts?
dbc.net
codecontracts> rewriter> verifier
rewriterinjects runtime checks
exampleT Pop(){ return _list.RemoveLast();}
preconditionT Pop(){Contract.Requires(!IsEmpty); return _list.RemoveLast();}
rewritten toT Pop(){if (__ContractsRuntime.insideContractEvaluation <= 4)  {    try    {      __ContractsRuntime.insideContractEvaluation++;      __ContractsRuntime.Requires(!this.IsEmpty, null, "!IsEmpty");    }    finally    {      __ContractsRuntime.insideContractEvaluation--;    }  }  return this._list.RemoveLast<T>();}
examplevoid Push(T t){  _list.Add(t);}
postconditionvoidPush(T t){Contract.Ensures(!IsEmpty);  _list.Add(t);}
rewritten tovoid Push(T t){  this._list.Add(t);if (__ContractsRuntime.insideContractEvaluation <= 4)  {    try    {__ContractsRuntime.insideContractEvaluation++;      __ContractsRuntime.Ensures(!this.IsEmpty, null, "!IsEmpty");    }    finally    {      __ContractsRuntime.insideContractEvaluation--;    }  }}
invariant[ContractInvariantMethod]private void Invariant(){ Contract.Invariant(Count >= 0);}
verifierperforms static checks
verifiervs>= premium
verifier
so farso good
a stricter contractT Pop(){Contract.Requires(!IsEmpty);  Contract.Ensures(Count < Contract.OldValue(Count));  Contract.Ensures(Contract.Result<T>()                 .Equals(Contract.OldValue(Top())));  return _list.RemoveLast();}
critique> ugly syntax> in method body> interface hack
yuck.
what isspec#?
preconditionT Pop()  requires !IsEmpty;{  return _list.RemoveLast();}
postconditionvoid Push(T t)  ensures !IsEmpty;{_list.Add(t);}
a stricter contractT Pop() requires !IsEmpty; ensures Count > old(Count); ensures result == old(Top());  {  return _list.RemoveLast();}
muchbetter!
lessonsyntax helps
conclusion
design by contract? yay!
ms code contracts? meh.
spec#?yay!

More Related Content

PDF
String searching
PPTX
Keypoints c strings
PDF
Bcsl 033 data and file structures lab s1-4
PPTX
Java JIT Optimization Research
PDF
Let us c chapter 4 solution
PDF
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
DOC
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
PDF
Let us c(by yashwant kanetkar) chapter 2 solution
String searching
Keypoints c strings
Bcsl 033 data and file structures lab s1-4
Java JIT Optimization Research
Let us c chapter 4 solution
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solution

What's hot (20)

PPT
Pointers+(2)
DOCX
C interview question answer 2
PPTX
Session12 pointers
PDF
Practiceproblems(1)
PPT
Doublylinklist
PDF
Hot C++: New Style of Arguments Passing
PPTX
Third session
DOCX
Let us c (by yashvant kanetkar) chapter 1 solution
PDF
175035 cse lab-05
PDF
C aptitude scribd
PPTX
Lecture 2: arrays and pointers
PDF
Extend GraphQL with directives
PDF
Applications of stack
DOC
C tech questions
PPT
Bca 2nd sem u-5 files & pointers
PPT
PPT
Pointers in C
PPT
Mca 2nd sem u-5 files & pointers
PPT
Lecture 18 - Pointers
PPTX
Introduction to F# for the C# developer
Pointers+(2)
C interview question answer 2
Session12 pointers
Practiceproblems(1)
Doublylinklist
Hot C++: New Style of Arguments Passing
Third session
Let us c (by yashvant kanetkar) chapter 1 solution
175035 cse lab-05
C aptitude scribd
Lecture 2: arrays and pointers
Extend GraphQL with directives
Applications of stack
C tech questions
Bca 2nd sem u-5 files & pointers
Pointers in C
Mca 2nd sem u-5 files & pointers
Lecture 18 - Pointers
Introduction to F# for the C# developer
Ad

Viewers also liked (9)

PPTX
Learning style
PPTX
Program understanding: What programmers really want
PPT
06. p ortogonal
PPTX
Work sample 1
PDF
Studi tentang vrptw
PPTX
Presentation edited
PPT
03.undas perancob,kerak,moper
PPTX
Thephilosophicalbackgroundofchildhoodeducationin 111202174236-phpapp01
Learning style
Program understanding: What programmers really want
06. p ortogonal
Work sample 1
Studi tentang vrptw
Presentation edited
03.undas perancob,kerak,moper
Thephilosophicalbackgroundofchildhoodeducationin 111202174236-phpapp01
Ad

Similar to Lyntale: MS Code Contracts (20)

PPTX
Code contracts by Dmytro Mindra
PPT
C#, What Is Next?
PPTX
Rock Your Code with Code Contracts
PPTX
Rock Your Code With Code Contracts -2013
PPTX
Code Contracts API In .Net
PPTX
.NET 4.0 Code Contracts (2010)
PPTX
Code Contracts API In .NET
PPT
Introduction to Spec#
PPTX
Category theory, Monads, and Duality in the world of (BIG) Data
PPT
How much do we know about Object-Oriented Programming?
PPTX
Enhance Your Code Quality with Code Contracts
PPTX
Program Verification / Automated Theorem Proving
PPTX
Software quality with Code Contracts and PEX - CodeCamp16oct2010
PPT
Whats new in_csharp4
ODP
JavaScript APIs In Focus
PPTX
Post Sharp Talk
PPTX
Overview of VS2010 and .NET 4.0
PPTX
Code Contracts
PPTX
Code contract
PPTX
Core .NET Framework 4.0 Enhancements
Code contracts by Dmytro Mindra
C#, What Is Next?
Rock Your Code with Code Contracts
Rock Your Code With Code Contracts -2013
Code Contracts API In .Net
.NET 4.0 Code Contracts (2010)
Code Contracts API In .NET
Introduction to Spec#
Category theory, Monads, and Duality in the world of (BIG) Data
How much do we know about Object-Oriented Programming?
Enhance Your Code Quality with Code Contracts
Program Verification / Automated Theorem Proving
Software quality with Code Contracts and PEX - CodeCamp16oct2010
Whats new in_csharp4
JavaScript APIs In Focus
Post Sharp Talk
Overview of VS2010 and .NET 4.0
Code Contracts
Code contract
Core .NET Framework 4.0 Enhancements

Recently uploaded (20)

PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Encapsulation theory and applications.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Cloud computing and distributed systems.
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
MYSQL Presentation for SQL database connectivity
PDF
KodekX | Application Modernization Development
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
NewMind AI Monthly Chronicles - July 2025
Encapsulation theory and applications.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
20250228 LYD VKU AI Blended-Learning.pptx
Electronic commerce courselecture one. Pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Big Data Technologies - Introduction.pptx
Network Security Unit 5.pdf for BCA BBA.
Encapsulation_ Review paper, used for researhc scholars
Chapter 3 Spatial Domain Image Processing.pdf
Machine learning based COVID-19 study performance prediction
Cloud computing and distributed systems.
The AUB Centre for AI in Media Proposal.docx
Dropbox Q2 2025 Financial Results & Investor Presentation
Advanced methodologies resolving dimensionality complications for autism neur...
Reach Out and Touch Someone: Haptics and Empathic Computing
MYSQL Presentation for SQL database connectivity
KodekX | Application Modernization Development

Lyntale: MS Code Contracts