SlideShare ist ein Scribd-Unternehmen logo
1 von 49
ms code contracts eih
what is a code contract?
caller
callee
contract
a weakcontract bool Equals(object o)
what is design 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 contract what does it expect? what does it guarantee? what does it maintain?
dbc tenets > prerequisites > postconditions > invariants
example stack
stack > Push(Tt) > T Pop() > T Top() > int Count > IsEmpty
dbc by hand
example T Pop() {   return _list.RemoveLast(); }
precondition T Pop() { Debug.Assert(!IsEmpty);   return _list.RemoveLast(); }
example voidPush(T t) {   _list.Add(t); }
postcondition voidPush(T t) {   try {     _list.Add(t); }   finally {     Debug.Assert(!IsEmpty);     } }
invariant Count >= 0
limitations tedious!
limitations clutters the code!
what is ms code contracts?
dbc.net
codecontracts > rewriter > verifier
rewriter injects runtime checks
example T Pop() {  return _list.RemoveLast(); }
precondition T Pop() { Contract.Requires(!IsEmpty);  return _list.RemoveLast(); }
rewritten to T Pop() { if (__ContractsRuntime.insideContractEvaluation <= 4)   {     try     {       __ContractsRuntime.insideContractEvaluation++;       __ContractsRuntime.Requires(!this.IsEmpty, null, "!IsEmpty");     }     finally     {       __ContractsRuntime.insideContractEvaluation--;     }   }   return this._list.RemoveLast<T>(); }
example void Push(T t) {   _list.Add(t); }
postcondition voidPush(T t) { Contract.Ensures(!IsEmpty);   _list.Add(t); }
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--;     }   } }
invariant [ContractInvariantMethod] private void Invariant() {  Contract.Invariant(Count >= 0); }
verifier performs static checks
verifier vs>= premium
verifier
so far so good
a stricter contract T 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 is spec#?
precondition T Pop()   requires !IsEmpty; {   return _list.RemoveLast(); }
postcondition void Push(T t)   ensures !IsEmpty; { _list.Add(t); }
a stricter contract T Pop()  requires !IsEmpty;  ensures Count > old(Count);  ensures result == old(Top());   {   return _list.RemoveLast(); }
muchbetter!
lesson syntax helps
conclusion
design by contract?  yay!
ms code contracts?  meh.
spec#? yay!

Weitere ähnliche Inhalte

Was ist angesagt?

C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
Amit Kapoor
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
ritu1806
 
C aptitude scribd
C aptitude scribdC aptitude scribd
C aptitude scribd
Amit Kapoor
 

Was ist angesagt? (20)

Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 
Session12 pointers
Session12 pointersSession12 pointers
Session12 pointers
 
Practiceproblems(1)
Practiceproblems(1)Practiceproblems(1)
Practiceproblems(1)
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
Hot C++: New Style of Arguments Passing
Hot C++: New Style of Arguments PassingHot C++: New Style of Arguments Passing
Hot C++: New Style of Arguments Passing
 
Third session
Third sessionThird session
Third session
 
Let us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solutionLet us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solution
 
175035 cse lab-05
175035 cse lab-05 175035 cse lab-05
175035 cse lab-05
 
C aptitude scribd
C aptitude scribdC aptitude scribd
C aptitude scribd
 
Lecture 2: arrays and pointers
Lecture 2: arrays and pointersLecture 2: arrays and pointers
Lecture 2: arrays and pointers
 
Extend GraphQL with directives
Extend GraphQL with directivesExtend GraphQL with directives
Extend GraphQL with directives
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
C tech questions
C tech questionsC tech questions
C tech questions
 
Bca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointersBca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointers
 
L05if
L05ifL05if
L05if
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Mca 2nd sem u-5 files & pointers
Mca 2nd  sem u-5 files & pointersMca 2nd  sem u-5 files & pointers
Mca 2nd sem u-5 files & pointers
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
Introduction to F# for the C# developer
Introduction to F# for the C# developerIntroduction to F# for the C# developer
Introduction to F# for the C# developer
 

Andere mochten auch

Learning style
Learning styleLearning style
Learning style
Shadid Al
 
Program understanding: What programmers really want
Program understanding: What programmers really wantProgram understanding: What programmers really want
Program understanding: What programmers really want
Einar Høst
 
Work sample 1
Work sample 1Work sample 1
Work sample 1
Shadid Al
 
Studi tentang vrptw
Studi tentang vrptwStudi tentang vrptw
Studi tentang vrptw
Jauhar Anam
 
Presentation edited
Presentation editedPresentation edited
Presentation edited
Shadid Al
 
03.undas perancob,kerak,moper
03.undas perancob,kerak,moper03.undas perancob,kerak,moper
03.undas perancob,kerak,moper
Jauhar Anam
 
Thephilosophicalbackgroundofchildhoodeducationin 111202174236-phpapp01
Thephilosophicalbackgroundofchildhoodeducationin 111202174236-phpapp01Thephilosophicalbackgroundofchildhoodeducationin 111202174236-phpapp01
Thephilosophicalbackgroundofchildhoodeducationin 111202174236-phpapp01
Shadid Al
 

Andere mochten auch (9)

Learning style
Learning styleLearning style
Learning style
 
Program understanding: What programmers really want
Program understanding: What programmers really wantProgram understanding: What programmers really want
Program understanding: What programmers really want
 
06. p ortogonal
06. p ortogonal06. p ortogonal
06. p ortogonal
 
Work sample 1
Work sample 1Work sample 1
Work sample 1
 
Studi tentang vrptw
Studi tentang vrptwStudi tentang vrptw
Studi tentang vrptw
 
Presentation edited
Presentation editedPresentation edited
Presentation edited
 
03.undas perancob,kerak,moper
03.undas perancob,kerak,moper03.undas perancob,kerak,moper
03.undas perancob,kerak,moper
 
Thephilosophicalbackgroundofchildhoodeducationin 111202174236-phpapp01
Thephilosophicalbackgroundofchildhoodeducationin 111202174236-phpapp01Thephilosophicalbackgroundofchildhoodeducationin 111202174236-phpapp01
Thephilosophicalbackgroundofchildhoodeducationin 111202174236-phpapp01
 
Greece4ever
Greece4everGreece4ever
Greece4ever
 

Ähnlich wie Lyntale: MS Code Contracts

Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
Mark Needham
 
Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimized
Woody Pewitt
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
Abed Bukhari
 
Generic Programming seminar
Generic Programming seminarGeneric Programming seminar
Generic Programming seminar
Gautam Roy
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
jahanullah
 
Lambda expressions
Lambda expressionsLambda expressions
Lambda expressions
Yuriy Seniuk
 
help me Java projectI put problem and my own code in the linkmy .pdf
help me Java projectI put problem and my own code in the linkmy .pdfhelp me Java projectI put problem and my own code in the linkmy .pdf
help me Java projectI put problem and my own code in the linkmy .pdf
arihantmum
 
maincpp include ListItemh include ltstringgt in.pdf
maincpp include ListItemh include ltstringgt in.pdfmaincpp include ListItemh include ltstringgt in.pdf
maincpp include ListItemh include ltstringgt in.pdf
abiwarmaa
 
Exception Handling1
Exception Handling1Exception Handling1
Exception Handling1
guest739536
 
Applying Generics
Applying GenericsApplying Generics
Applying Generics
Bharat17485
 

Ähnlich wie Lyntale: MS Code Contracts (20)

C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
 
Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#
 
Vb.Net 01 To 03 Summary Upload
Vb.Net 01 To 03 Summary UploadVb.Net 01 To 03 Summary Upload
Vb.Net 01 To 03 Summary Upload
 
Preprocessor Programming
Preprocessor ProgrammingPreprocessor Programming
Preprocessor Programming
 
Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimized
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
 
A scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ codeA scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ code
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
Generic Programming seminar
Generic Programming seminarGeneric Programming seminar
Generic Programming seminar
 
Loops
LoopsLoops
Loops
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
Lambda expressions
Lambda expressionsLambda expressions
Lambda expressions
 
Functional DDD
Functional DDDFunctional DDD
Functional DDD
 
Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd Study
 
help me Java projectI put problem and my own code in the linkmy .pdf
help me Java projectI put problem and my own code in the linkmy .pdfhelp me Java projectI put problem and my own code in the linkmy .pdf
help me Java projectI put problem and my own code in the linkmy .pdf
 
maincpp include ListItemh include ltstringgt in.pdf
maincpp include ListItemh include ltstringgt in.pdfmaincpp include ListItemh include ltstringgt in.pdf
maincpp include ListItemh include ltstringgt in.pdf
 
Exception Handling1
Exception Handling1Exception Handling1
Exception Handling1
 
Applying Generics
Applying GenericsApplying Generics
Applying Generics
 

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Kürzlich hochgeladen (20)

A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

Lyntale: MS Code Contracts