SlideShare a Scribd company logo
1 of 21
design by 
contract 
sabre .net 
community 
kraków sep 9 
2014 dariuszwozniak.net
agenda defensive programming design by contract 
• What is: Defensive Programming 
• What is: Design by Contract (DBC) and Code Contracts 
• Benefits of DBC 
• History of DBC 
• Code Contracts in C# 
• Examples 
• Live Demo 
• Summary 
• Q&A
agenda defensive programming design by contract 
Syntax Correctness 
• Verified by a compiler 
Semantic Correctness 
• Verified in a runtime 
• Major cause of bugs 
• Examples: 
• Count() >= 0 
• age must be in range [0; 122] 
• Obj cannot be Null
agenda defensive programming design by contract 
GetRoom(Hotel hotel); 
Problem: 
How to check whether it is NULL or not? 
• if (hotel == null) throw new ArgumentNullException(”hotel”); 
• Debug.Assert(hotel != null); 
• Trace.Assert(hotel != null); 
- Configurable (DEBUGRELEASEetc.) 
- Compile check 
• Contract.Requires<ArgumentNullException>(hotel != null, ”hotel”);
agenda defensive programming design by contract 
• design by contract is a software correctness 
methodology 
• it uses preconditions and postconditions to 
document (or programmatically assert) the change 
in state caused by a piece of a program 
source: http://c2.com/cgi/wiki?DesignByContract
agenda defensive programming design by contract 
static (compile-time) and/or runtime checking 
• precondition 
• condition checked on entry to method 
• postcondition 
• condition checked on exit of method 
• object invariant 
• condition that always should be true
benefits history examples summary references 
• static verification 
• automatic testing tools 
• code documentation 
• contracts as documentation 
• contracts added to documentation 
• cleaner code 
• improved feedback loop 
• short learning curve
benefits history examples summary references 
1986: Eiffel
benefits history examples summary references 
1986: Eiffel 2004: Spec#
benefits history examples summary references 
1986: Eiffel 2004: Spec# 2008: Code Contracts in .NET
benefits history examples summary references 
1986: Eiffel 2004: Spec# 2008: Code Contracts in .NET 
• part of the library since .NET 4.0 
• static and runtime checking (configurable per project) 
• inheritable contracts 
• support for abstract classes and interfaces
benefits history examples summary references 
1986: Eiffel 2004: Spec# 2008: Code Contracts in .NET 
• generate API documentation 
• hooks into XML documentation and inserts contract 
requirements (requires, ensures) 
• automatically suggests missing contracts 
• resharper support
examples
benefits history examples summary references 
preconditions 
public int Add(int a, int b) 
{ 
Contract.Requires<ArgumentOutOfRangeException>(a >= 0); 
Contract.Requires<ArgumentOutOfRangeException>(b >= 0); 
// main logic 
}
benefits history examples summary references 
postconditions 
public int Add(int a, int b) 
{ 
// pre-conditions 
Contract.Ensures(Contract.Result<int>() >= 0); 
// main logic 
}
benefits history examples summary references 
object invariants 
[ContractInvariantMethod] 
private void CheckIfLastResultIsInRange() 
{ 
Contract.Invariant(lastResult >= 0); 
}
// demo
benefits history examples summary references 
• defensive programming 
• software correctness 
• static and runtime checking of 
• preconditions 
• postconditions 
• object invariants 
• documents and asserts changes in a state of a 
program
benefits history examples summary references 
• MSDN: Code Contracts http://msdn.microsoft.com/en-us/library/dd264808%28v=vs.110%29.aspx 
• Using the Spec# Language, Methodology, and Tools to Write Bug-Free Programs [2009] 
• Mike Frederick: Code Contracts in .NET 4 — SVNUG Presentation [December 2011] 
• Code Contracts is the next coding practice you should learn and use 
http://codebetter.com/patricksmacchia/2013/12/18/code-contracts-is-the-next-coding-practice-you- 
should-learn-and-use/ 
• Clarence Bakirtzidis: Code Contracts API In .NET 
• http://c2.com/cgi/wiki?DesignByContract 
• Jon Skeet: C# in Depth (2nd ed.)
questions
thank you 
github.com/dariusz-wozniak/dbc-demo 
dariusz.wozniak@sabre.com 
dariuszwozniak.net

More Related Content

What's hot

ITkonekt 2019 | Robert C. Martin (Uncle Bob), Clean Architecture and Design
ITkonekt 2019 | Robert C. Martin (Uncle Bob), Clean Architecture and DesignITkonekt 2019 | Robert C. Martin (Uncle Bob), Clean Architecture and Design
ITkonekt 2019 | Robert C. Martin (Uncle Bob), Clean Architecture and DesignErginBilgin3
 
Types of Blockchains
Types of BlockchainsTypes of Blockchains
Types of BlockchainsVikram Khanna
 
The aggregate is dead! Long live the aggregate! - SpringIO.pdf
The aggregate is dead! Long live the aggregate! - SpringIO.pdfThe aggregate is dead! Long live the aggregate! - SpringIO.pdf
The aggregate is dead! Long live the aggregate! - SpringIO.pdfSara Pellegrini
 
Device Twins, Digital Twins and Device Shadow
Device Twins, Digital Twins and Device ShadowDevice Twins, Digital Twins and Device Shadow
Device Twins, Digital Twins and Device ShadowEstelle Auberix
 
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023Steve Pember
 
Smart contracts using web3.js
Smart contracts using web3.jsSmart contracts using web3.js
Smart contracts using web3.jsFelix Crisan
 
Graphical User Interface (Gui)
Graphical User Interface (Gui)Graphical User Interface (Gui)
Graphical User Interface (Gui)Bilal Amjad
 
你所不知道的 DDD - 文件驅動開發
你所不知道的 DDD - 文件驅動開發你所不知道的 DDD - 文件驅動開發
你所不知道的 DDD - 文件驅動開發志龍 陳
 
Non-fungible tokens (nfts)
Non-fungible tokens (nfts)Non-fungible tokens (nfts)
Non-fungible tokens (nfts)Gene Leybzon
 
JVM: A Platform for Multiple Languages
JVM: A Platform for Multiple LanguagesJVM: A Platform for Multiple Languages
JVM: A Platform for Multiple LanguagesKris Mok
 
Building a NFT Marketplace DApp
Building a NFT Marketplace DAppBuilding a NFT Marketplace DApp
Building a NFT Marketplace DAppThanh Nguyen
 
Vertical Slicing Architectures
Vertical Slicing ArchitecturesVertical Slicing Architectures
Vertical Slicing ArchitecturesVictor Rentea
 

What's hot (20)

ITkonekt 2019 | Robert C. Martin (Uncle Bob), Clean Architecture and Design
ITkonekt 2019 | Robert C. Martin (Uncle Bob), Clean Architecture and DesignITkonekt 2019 | Robert C. Martin (Uncle Bob), Clean Architecture and Design
ITkonekt 2019 | Robert C. Martin (Uncle Bob), Clean Architecture and Design
 
Types of Blockchains
Types of BlockchainsTypes of Blockchains
Types of Blockchains
 
The aggregate is dead! Long live the aggregate! - SpringIO.pdf
The aggregate is dead! Long live the aggregate! - SpringIO.pdfThe aggregate is dead! Long live the aggregate! - SpringIO.pdf
The aggregate is dead! Long live the aggregate! - SpringIO.pdf
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
 
Device Twins, Digital Twins and Device Shadow
Device Twins, Digital Twins and Device ShadowDevice Twins, Digital Twins and Device Shadow
Device Twins, Digital Twins and Device Shadow
 
Modular Monoliths with Nx
Modular Monoliths with NxModular Monoliths with Nx
Modular Monoliths with Nx
 
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
 
class diagram
class diagramclass diagram
class diagram
 
Smart contracts using web3.js
Smart contracts using web3.jsSmart contracts using web3.js
Smart contracts using web3.js
 
Graphical User Interface (Gui)
Graphical User Interface (Gui)Graphical User Interface (Gui)
Graphical User Interface (Gui)
 
Introduction to Blockchain
Introduction to BlockchainIntroduction to Blockchain
Introduction to Blockchain
 
你所不知道的 DDD - 文件驅動開發
你所不知道的 DDD - 文件驅動開發你所不知道的 DDD - 文件驅動開發
你所不知道的 DDD - 文件驅動開發
 
Activity diagrams
Activity diagramsActivity diagrams
Activity diagrams
 
Non-fungible tokens (nfts)
Non-fungible tokens (nfts)Non-fungible tokens (nfts)
Non-fungible tokens (nfts)
 
JVM: A Platform for Multiple Languages
JVM: A Platform for Multiple LanguagesJVM: A Platform for Multiple Languages
JVM: A Platform for Multiple Languages
 
Building a NFT Marketplace DApp
Building a NFT Marketplace DAppBuilding a NFT Marketplace DApp
Building a NFT Marketplace DApp
 
Vertical Slicing Architectures
Vertical Slicing ArchitecturesVertical Slicing Architectures
Vertical Slicing Architectures
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 
Windowforms controls c#
Windowforms controls c#Windowforms controls c#
Windowforms controls c#
 
What is design pattern
What is design patternWhat is design pattern
What is design pattern
 

Similar to Design by Contract | Code Contracts in C# .NET

Workshop: .NET Code Contracts
Workshop: .NET Code ContractsWorkshop: .NET Code Contracts
Workshop: .NET Code ContractsRainer Stropek
 
Rock Your Code With Code Contracts -2013
Rock Your Code With Code Contracts -2013Rock Your Code With Code Contracts -2013
Rock Your Code With Code Contracts -2013David McCarter
 
Enhance Your Code Quality with Code Contracts
Enhance Your Code Quality with Code ContractsEnhance Your Code Quality with Code Contracts
Enhance Your Code Quality with Code ContractsEran Stiller
 
.NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010).NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010)Koen Metsu
 
How to Create a Service in Choreo
How to Create a Service in ChoreoHow to Create a Service in Choreo
How to Create a Service in ChoreoWSO2
 
Tech talk specflow_bddx_hassa_nagy
Tech talk specflow_bddx_hassa_nagyTech talk specflow_bddx_hassa_nagy
Tech talk specflow_bddx_hassa_nagySkills Matter
 
Code quality
Code qualityCode quality
Code qualityProvectus
 
Pushing the Boundary of Mostly Automatic Program Proof
Pushing the Boundary of Mostly Automatic Program ProofPushing the Boundary of Mostly Automatic Program Proof
Pushing the Boundary of Mostly Automatic Program ProofAdaCore
 
“ASP.NET Core. Features and architecture”
“ASP.NET Core. Features and architecture” “ASP.NET Core. Features and architecture”
“ASP.NET Core. Features and architecture” HYS Enterprise
 
Rock Your Code with Code Contracts
Rock Your Code with Code ContractsRock Your Code with Code Contracts
Rock Your Code with Code ContractsDavid McCarter
 
Integrating Infrastructure as Code into a Continuous Delivery Pipeline | AWS ...
Integrating Infrastructure as Code into a Continuous Delivery Pipeline | AWS ...Integrating Infrastructure as Code into a Continuous Delivery Pipeline | AWS ...
Integrating Infrastructure as Code into a Continuous Delivery Pipeline | AWS ...Amazon Web Services
 
Crafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an ArchitectCrafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an ArchitectColdFusionConference
 
Field injection, type safe configuration, and more new goodies in Declarative...
Field injection, type safe configuration, and more new goodies in Declarative...Field injection, type safe configuration, and more new goodies in Declarative...
Field injection, type safe configuration, and more new goodies in Declarative...bjhargrave
 
"How to create an infrastructure in .NET", Leonid Chetverikov
"How to create an infrastructure in .NET", Leonid Chetverikov"How to create an infrastructure in .NET", Leonid Chetverikov
"How to create an infrastructure in .NET", Leonid ChetverikovFwdays
 
MVC 6 - the new unified Web programming model
MVC 6 - the new unified Web programming modelMVC 6 - the new unified Web programming model
MVC 6 - the new unified Web programming modelAlex Thissen
 

Similar to Design by Contract | Code Contracts in C# .NET (20)

Code Contracts API In .Net
Code Contracts API In .NetCode Contracts API In .Net
Code Contracts API In .Net
 
Code Contracts API In .NET
Code Contracts API In .NETCode Contracts API In .NET
Code Contracts API In .NET
 
Workshop: .NET Code Contracts
Workshop: .NET Code ContractsWorkshop: .NET Code Contracts
Workshop: .NET Code Contracts
 
Rock Your Code With Code Contracts -2013
Rock Your Code With Code Contracts -2013Rock Your Code With Code Contracts -2013
Rock Your Code With Code Contracts -2013
 
Enhance Your Code Quality with Code Contracts
Enhance Your Code Quality with Code ContractsEnhance Your Code Quality with Code Contracts
Enhance Your Code Quality with Code Contracts
 
Tdd,Ioc
Tdd,IocTdd,Ioc
Tdd,Ioc
 
.NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010).NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010)
 
Conformiq Tutorial
Conformiq TutorialConformiq Tutorial
Conformiq Tutorial
 
ServerLess by usama Azure fuctions.pptx
ServerLess by usama Azure fuctions.pptxServerLess by usama Azure fuctions.pptx
ServerLess by usama Azure fuctions.pptx
 
How to Create a Service in Choreo
How to Create a Service in ChoreoHow to Create a Service in Choreo
How to Create a Service in Choreo
 
Tech talk specflow_bddx_hassa_nagy
Tech talk specflow_bddx_hassa_nagyTech talk specflow_bddx_hassa_nagy
Tech talk specflow_bddx_hassa_nagy
 
Code quality
Code qualityCode quality
Code quality
 
Pushing the Boundary of Mostly Automatic Program Proof
Pushing the Boundary of Mostly Automatic Program ProofPushing the Boundary of Mostly Automatic Program Proof
Pushing the Boundary of Mostly Automatic Program Proof
 
“ASP.NET Core. Features and architecture”
“ASP.NET Core. Features and architecture” “ASP.NET Core. Features and architecture”
“ASP.NET Core. Features and architecture”
 
Rock Your Code with Code Contracts
Rock Your Code with Code ContractsRock Your Code with Code Contracts
Rock Your Code with Code Contracts
 
Integrating Infrastructure as Code into a Continuous Delivery Pipeline | AWS ...
Integrating Infrastructure as Code into a Continuous Delivery Pipeline | AWS ...Integrating Infrastructure as Code into a Continuous Delivery Pipeline | AWS ...
Integrating Infrastructure as Code into a Continuous Delivery Pipeline | AWS ...
 
Crafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an ArchitectCrafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an Architect
 
Field injection, type safe configuration, and more new goodies in Declarative...
Field injection, type safe configuration, and more new goodies in Declarative...Field injection, type safe configuration, and more new goodies in Declarative...
Field injection, type safe configuration, and more new goodies in Declarative...
 
"How to create an infrastructure in .NET", Leonid Chetverikov
"How to create an infrastructure in .NET", Leonid Chetverikov"How to create an infrastructure in .NET", Leonid Chetverikov
"How to create an infrastructure in .NET", Leonid Chetverikov
 
MVC 6 - the new unified Web programming model
MVC 6 - the new unified Web programming modelMVC 6 - the new unified Web programming model
MVC 6 - the new unified Web programming model
 

Recently uploaded

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durbanmasabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburgmasabamasaba
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationShrmpro
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 

Recently uploaded (20)

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 

Design by Contract | Code Contracts in C# .NET

  • 1. design by contract sabre .net community kraków sep 9 2014 dariuszwozniak.net
  • 2. agenda defensive programming design by contract • What is: Defensive Programming • What is: Design by Contract (DBC) and Code Contracts • Benefits of DBC • History of DBC • Code Contracts in C# • Examples • Live Demo • Summary • Q&A
  • 3. agenda defensive programming design by contract Syntax Correctness • Verified by a compiler Semantic Correctness • Verified in a runtime • Major cause of bugs • Examples: • Count() >= 0 • age must be in range [0; 122] • Obj cannot be Null
  • 4. agenda defensive programming design by contract GetRoom(Hotel hotel); Problem: How to check whether it is NULL or not? • if (hotel == null) throw new ArgumentNullException(”hotel”); • Debug.Assert(hotel != null); • Trace.Assert(hotel != null); - Configurable (DEBUGRELEASEetc.) - Compile check • Contract.Requires<ArgumentNullException>(hotel != null, ”hotel”);
  • 5. agenda defensive programming design by contract • design by contract is a software correctness methodology • it uses preconditions and postconditions to document (or programmatically assert) the change in state caused by a piece of a program source: http://c2.com/cgi/wiki?DesignByContract
  • 6. agenda defensive programming design by contract static (compile-time) and/or runtime checking • precondition • condition checked on entry to method • postcondition • condition checked on exit of method • object invariant • condition that always should be true
  • 7. benefits history examples summary references • static verification • automatic testing tools • code documentation • contracts as documentation • contracts added to documentation • cleaner code • improved feedback loop • short learning curve
  • 8. benefits history examples summary references 1986: Eiffel
  • 9. benefits history examples summary references 1986: Eiffel 2004: Spec#
  • 10. benefits history examples summary references 1986: Eiffel 2004: Spec# 2008: Code Contracts in .NET
  • 11. benefits history examples summary references 1986: Eiffel 2004: Spec# 2008: Code Contracts in .NET • part of the library since .NET 4.0 • static and runtime checking (configurable per project) • inheritable contracts • support for abstract classes and interfaces
  • 12. benefits history examples summary references 1986: Eiffel 2004: Spec# 2008: Code Contracts in .NET • generate API documentation • hooks into XML documentation and inserts contract requirements (requires, ensures) • automatically suggests missing contracts • resharper support
  • 14. benefits history examples summary references preconditions public int Add(int a, int b) { Contract.Requires<ArgumentOutOfRangeException>(a >= 0); Contract.Requires<ArgumentOutOfRangeException>(b >= 0); // main logic }
  • 15. benefits history examples summary references postconditions public int Add(int a, int b) { // pre-conditions Contract.Ensures(Contract.Result<int>() >= 0); // main logic }
  • 16. benefits history examples summary references object invariants [ContractInvariantMethod] private void CheckIfLastResultIsInRange() { Contract.Invariant(lastResult >= 0); }
  • 18. benefits history examples summary references • defensive programming • software correctness • static and runtime checking of • preconditions • postconditions • object invariants • documents and asserts changes in a state of a program
  • 19. benefits history examples summary references • MSDN: Code Contracts http://msdn.microsoft.com/en-us/library/dd264808%28v=vs.110%29.aspx • Using the Spec# Language, Methodology, and Tools to Write Bug-Free Programs [2009] • Mike Frederick: Code Contracts in .NET 4 — SVNUG Presentation [December 2011] • Code Contracts is the next coding practice you should learn and use http://codebetter.com/patricksmacchia/2013/12/18/code-contracts-is-the-next-coding-practice-you- should-learn-and-use/ • Clarence Bakirtzidis: Code Contracts API In .NET • http://c2.com/cgi/wiki?DesignByContract • Jon Skeet: C# in Depth (2nd ed.)
  • 21. thank you github.com/dariusz-wozniak/dbc-demo dariusz.wozniak@sabre.com dariuszwozniak.net

Editor's Notes

  1. pl: asercje - warunki poczatkowe - warunki koncowe - niezmienniki klas http://www.cs.put.poznan.pl/dbrzezinski/teaching/po/7%20-%20Programowanie%20przez%20kontrakt.pdf
  2. poprawność składniowa poprawność semantyczna
  3. I've been trained to believe that throwing the ArgumentNullException is "correct" but an "Object reference not set to an instance of an object" error means I have a bug. Why? Suppose I call method M(x) that you wrote. I pass null. I get an ArgumentNullException with the name set to "x". That exception unambiguously means that I have a bug; I should not have passed null for x. Suppose I call a method M that you wrote. I pass null. I get a null deref exception. How the heck am I supposed to know whether I have a bug or you have a bug or some third party library that you called on my behalf has a bug? I know nothing about whether I need to fix my code or call you up to tell you to fix yours. Both exceptions are indicative of bugs; neither should ever be thrown in production code. The question is which exception communicates who has the bug better to the person doing the debugging? Null deref exception tells you almost nothing; argument null exception tells you a lot. Be kind to your users; throw exceptions that enable them to learn from their mistakes. http://programmers.stackexchange.com/questions/121121/how-does-throwing-an-argumentnullexception-help
  4. The pre- and postcondition technique originated with the work of TonyHoare, whose 1969 Communications of the ACM paper described program semantics using such assertions. Hoare's 1972 Acta Informatica paper described the use of representation invariants and abstraction functions to prove correctness of abstract data types. Pre- and postconditions were first supported natively in a language in BarbaraLiskovs CLU (circa 1974 - 1977).
  5. The Difference between Assert and Assume is that with Assert the static checker tries to prove it, with assume the static checker simply assumes it's true. So if you have a error in your logic, Assert will warn you about it, Assume will simply ignore it. [http://social.msdn.microsoft.com/Forums/en-US/f0039acf-cc08-480c-ac5f-01f40345c04d/contractassert-or-contractassume-?forum=codecontracts]