SlideShare a Scribd company logo
Code Contracts
   Enhance Your Code Quality with Code Contracts

Eran Stiller
Senior Software
Architect, Founder
March 20, 2013
About Me
•   Software architect, consultant and instructor
•   Technology addict
•   10 years of experience
•   .NET and Native Windows Programming
•   Specializes in large-scale, server-side, highly-concurrent systems
•   Co-Founder at CodeValue Ltd.
•   http://stiller.co.il/blog
Agenda
• Back to Basics
• Design by Contract
• Code Contracts
  – Preconditions
  – Postconditions
  – Invariants
• Internals
• External Tools
Back to Basics
The If…Throw Approach
public class MyClass
{
       public void SomeMethod(string s, int i)
       {
              if (s == null)
                     throw new ArgumentNullException(
                            "s", "Your input string cannot be null!");
              if (i < 0)
                     throw new ArgumentException(
                            "Your input must be non-negative!", "i");

             // Your code here
        }
}
The If…Throw Approach
• Issues
  – Cumbersome – lots of code
  – Indistinguishable from method code
  – Does not apply to interfaces
  – Does not apply across object inheritance
  – Not visible at call site
  – Not part of the contract
Design by Contract
“Design by contract (DbC) (…) is an approach for
designing software. It prescribes that software
designers should define formal, precise and verifiable interface
specifications for software components, which extend the
ordinary definition of abstract data types with preconditions,
postconditions and invariants. These specifications are referred
to as "contracts", in accordance with a conceptual metaphor
with the conditions and obligations of business contracts.”
(Wikipedia)
Enter Code Contracts
• Microsoft’s Design-by-Contract
  implementation for .NET
  – A subset of Spec#
• Language-agnostic way to express coding assumptions
• Checked documentation of your API
  – Improved testability
  – Static verification
  – API documentation
Requirements
• .NET 4.0/4.5
• Visual Studio 2010/2012
  – Any edition except “Express” edition
• Download and install from Microsoft Research
  – http://research.microsoft.com/en-us/projects/contracts/
• Allowed for commercial use

• In addition
  – Support exists for .NET 3.5 and VS 2008 as well
  – Contracts API is part of an external assembly
Preconditions
• Define what your API expects
  – Parameter validation
  – Object state
  – Environment condition
  – Anything the caller can check!
Postconditions
• Define what the caller can expect from your API
  – Return value constraints
  – Exception constraints
  – Object state after call
Invariants
• Define the rules which govern your object’s behavior
  – Object state
  – Field & property values
  – Things that always hold true
Enough Talking. Show Me the Code!
Should I Use It? It Depends…
• Pros
  –   Reduce bugs
  –   Static verification
  –   API documentation
  –   Improved testability
  –   Shallow learning curve
• Cons
  –   Experimental project
  –   Static verifier less than perfect
  –   Some issues with VS2012
  –   Compilation time increase
There is a Middle Way!
public class MyClass
{
        public void SomeMethod(string s, int i)
        {
                if (s == null)
                        throw new ArgumentNullException(
                                "s", "Your input string cannot be null!");
                if (i < 0)
                        throw new ArgumentException(
                                "Your input must be non-negative!", "i");
                 Contract.EndContractBlock();

               // Your code here
        }
}
How Can I Choose?
 From the
 User Manual
Code Contracts Internals
• Contract Rewriter
   – ccrewrite
• Static Verifier
   – cccheck
• API Documentation
   – ccdoc
• Ships with contract assemblies for many BCL types & methods
Code Contracts Internals
Code Contracts Editor Extension
• Visual Studio 2010 Only
  – 2012 support still pending
  – http://tinyurl.com/CCEditorExtensions
• Has some stability issues
Summary
• A contract is more than an Interface
• Design by Contract is a design approach
• Code Contracts enables Design by Contract on .NET
    – Preconditions, Postconditions & Invariants
•   An experimental project from Microsoft Research
•   Rewrites your application’s IL code
•   Integrates with Editor Extensions & Pex
•   Give it a try!
Resources
• My Blog
  – http://stiller.co.il/blog
• Code Contracts
  – http://research.microsoft.com/en-us/projects/contracts/
  – http://research.microsoft.com/en-us/projects/contracts/userdoc.pdf
  – http://social.msdn.microsoft.com/Forums/en-US/codecontracts/threads
• Design by Contract
  – http://en.wikipedia.org/wiki/Design_by_contract
• Pex
  – http://research.microsoft.com/en-us/projects/pex/
Presenter contact details
c: +972-54-6160646
e: erans@codevalue.net
b: stiller.co.il/blog
w: www.codevalue.net

More Related Content

What's hot

Writing Testable Code in SharePoint
Writing Testable Code in SharePointWriting Testable Code in SharePoint
Writing Testable Code in SharePoint
Tim McCarthy
 
Test driven development
Test driven developmentTest driven development
Test driven development
Dennis Ahaus
 
Code review
Code reviewCode review
Code review
dqpi
 
Coding Standard And Code Review
Coding Standard And Code ReviewCoding Standard And Code Review
Coding Standard And Code Review
Milan Vukoje
 
Current Testing Challenges Ireland
Current Testing Challenges IrelandCurrent Testing Challenges Ireland
Current Testing Challenges Ireland
David O'Dowd
 
Documenting code yapceu2016
Documenting code yapceu2016Documenting code yapceu2016
Documenting code yapceu2016
Søren Lund
 
Clean architecture
Clean architectureClean architecture
Clean architecture
Travis Frisinger
 
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Søren Lund
 
Design for Testability in Practice
Design for Testability in PracticeDesign for Testability in Practice
Design for Testability in Practice
TechWell
 
Mca 02 year_exp_unit_automation_testing_ldra_rtrt_c -
Mca 02 year_exp_unit_automation_testing_ldra_rtrt_c -Mca 02 year_exp_unit_automation_testing_ldra_rtrt_c -
Mca 02 year_exp_unit_automation_testing_ldra_rtrt_c -
sandeep kumar gupta
 
LogiLogicless UI prototyping with Node.js | SuperSpeaker@CodeCamp Iasi, 2014
LogiLogicless UI prototyping with Node.js | SuperSpeaker@CodeCamp Iasi, 2014LogiLogicless UI prototyping with Node.js | SuperSpeaker@CodeCamp Iasi, 2014
LogiLogicless UI prototyping with Node.js | SuperSpeaker@CodeCamp Iasi, 2014
Endava
 
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
Theo Jungeblut
 
Code Review Best Practices
Code Review Best PracticesCode Review Best Practices
Code Review Best Practices
Trisha Gee
 
API Design - developing for developers
API Design - developing for developersAPI Design - developing for developers
API Design - developing for developers
Joy George
 
Lessons Learnt from Backend Systems Development
Lessons Learnt from Backend Systems DevelopmentLessons Learnt from Backend Systems Development
Lessons Learnt from Backend Systems Development
Michal Juhas
 
TDD and BDD in Sky Deutschland
TDD and BDD in Sky DeutschlandTDD and BDD in Sky Deutschland
TDD and BDD in Sky Deutschland
Murphy Meng
 
Open sourcing Adyen’s API explorer
Open sourcing Adyen’s API explorerOpen sourcing Adyen’s API explorer
Open sourcing Adyen’s API explorer
Pronovix
 
BDD and Behave
BDD and BehaveBDD and Behave
BDD and Behave
Adam Englander
 
Beyond Unit Testing
Beyond Unit TestingBeyond Unit Testing
Beyond Unit Testing
Søren Lund
 

What's hot (19)

Writing Testable Code in SharePoint
Writing Testable Code in SharePointWriting Testable Code in SharePoint
Writing Testable Code in SharePoint
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Code review
Code reviewCode review
Code review
 
Coding Standard And Code Review
Coding Standard And Code ReviewCoding Standard And Code Review
Coding Standard And Code Review
 
Current Testing Challenges Ireland
Current Testing Challenges IrelandCurrent Testing Challenges Ireland
Current Testing Challenges Ireland
 
Documenting code yapceu2016
Documenting code yapceu2016Documenting code yapceu2016
Documenting code yapceu2016
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016
 
Design for Testability in Practice
Design for Testability in PracticeDesign for Testability in Practice
Design for Testability in Practice
 
Mca 02 year_exp_unit_automation_testing_ldra_rtrt_c -
Mca 02 year_exp_unit_automation_testing_ldra_rtrt_c -Mca 02 year_exp_unit_automation_testing_ldra_rtrt_c -
Mca 02 year_exp_unit_automation_testing_ldra_rtrt_c -
 
LogiLogicless UI prototyping with Node.js | SuperSpeaker@CodeCamp Iasi, 2014
LogiLogicless UI prototyping with Node.js | SuperSpeaker@CodeCamp Iasi, 2014LogiLogicless UI prototyping with Node.js | SuperSpeaker@CodeCamp Iasi, 2014
LogiLogicless UI prototyping with Node.js | SuperSpeaker@CodeCamp Iasi, 2014
 
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
 
Code Review Best Practices
Code Review Best PracticesCode Review Best Practices
Code Review Best Practices
 
API Design - developing for developers
API Design - developing for developersAPI Design - developing for developers
API Design - developing for developers
 
Lessons Learnt from Backend Systems Development
Lessons Learnt from Backend Systems DevelopmentLessons Learnt from Backend Systems Development
Lessons Learnt from Backend Systems Development
 
TDD and BDD in Sky Deutschland
TDD and BDD in Sky DeutschlandTDD and BDD in Sky Deutschland
TDD and BDD in Sky Deutschland
 
Open sourcing Adyen’s API explorer
Open sourcing Adyen’s API explorerOpen sourcing Adyen’s API explorer
Open sourcing Adyen’s API explorer
 
BDD and Behave
BDD and BehaveBDD and Behave
BDD and Behave
 
Beyond Unit Testing
Beyond Unit TestingBeyond Unit Testing
Beyond Unit Testing
 

Similar to Enhance Your Code Quality with Code Contracts

NetWork - 15.10.2011 - Applied code generation in .NET
NetWork - 15.10.2011 - Applied code generation in .NET NetWork - 15.10.2011 - Applied code generation in .NET
NetWork - 15.10.2011 - Applied code generation in .NET
Dmytro Mindra
 
Crafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an ArchitectCrafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an Architect
ColdFusionConference
 
Clean Code Part III - Craftsmanship at SoCal Code Camp
Clean Code Part III - Craftsmanship at SoCal Code CampClean Code Part III - Craftsmanship at SoCal Code Camp
Clean Code Part III - Craftsmanship at SoCal Code Camp
Theo Jungeblut
 
Binary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code Testing
Binary Studio
 
Old code doesn't stink
Old code doesn't stinkOld code doesn't stink
Old code doesn't stink
Martin Gutenbrunner
 
10 Reasons You MUST Consider Pattern-Aware Programming
10 Reasons You MUST Consider Pattern-Aware Programming10 Reasons You MUST Consider Pattern-Aware Programming
10 Reasons You MUST Consider Pattern-Aware Programming
PostSharp Technologies
 
Code quality
Code qualityCode quality
Code quality
Provectus
 
NET Code Testing
NET Code TestingNET Code Testing
NET Code Testing
Kirill Miroshnichenko
 
Enterprise Node - Code Quality
Enterprise Node - Code QualityEnterprise Node - Code Quality
Enterprise Node - Code Quality
Kurtis Kemple
 
CBDW2014 - Behavior Driven Development with TestBox
CBDW2014 - Behavior Driven Development with TestBoxCBDW2014 - Behavior Driven Development with TestBox
CBDW2014 - Behavior Driven Development with TestBox
Ortus Solutions, Corp
 
Coding Naked
Coding NakedCoding Naked
Coding Naked
Caleb Jenkins
 
A Tale of Two Pizzas: Accelerating Software Delivery with AWS Developer Tools
A Tale of Two Pizzas: Accelerating Software Delivery with AWS Developer ToolsA Tale of Two Pizzas: Accelerating Software Delivery with AWS Developer Tools
A Tale of Two Pizzas: Accelerating Software Delivery with AWS Developer Tools
Amazon Web Services
 
Enter the mind of an Agile Developer
Enter the mind of an Agile DeveloperEnter the mind of an Agile Developer
Enter the mind of an Agile Developer
BSGAfrica
 
ENT201 A Tale of Two Pizzas: Accelerating Software Delivery with AWS Develope...
ENT201 A Tale of Two Pizzas: Accelerating Software Delivery with AWS Develope...ENT201 A Tale of Two Pizzas: Accelerating Software Delivery with AWS Develope...
ENT201 A Tale of Two Pizzas: Accelerating Software Delivery with AWS Develope...
Amazon Web Services
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
Steven Smith
 
Autoframework design
Autoframework designAutoframework design
Autoframework design
Forge Events
 
Writing less code with Serverless on AWS at AWS User Group Nairobi
Writing less code with Serverless on AWS at AWS User Group NairobiWriting less code with Serverless on AWS at AWS User Group Nairobi
Writing less code with Serverless on AWS at AWS User Group Nairobi
Vadym Kazulkin
 
AspectMock
AspectMockAspectMock
AspectMock
Bryce Embry
 
Improving the Quality of Existing Software - DevIntersection April 2016
Improving the Quality of Existing Software - DevIntersection April 2016Improving the Quality of Existing Software - DevIntersection April 2016
Improving the Quality of Existing Software - DevIntersection April 2016
Steven Smith
 
Wavect - Your long-term partner for sophisticated and maintainable software
Wavect - Your long-term partner for sophisticated and maintainable softwareWavect - Your long-term partner for sophisticated and maintainable software
Wavect - Your long-term partner for sophisticated and maintainable software
Kevin Riedl
 

Similar to Enhance Your Code Quality with Code Contracts (20)

NetWork - 15.10.2011 - Applied code generation in .NET
NetWork - 15.10.2011 - Applied code generation in .NET NetWork - 15.10.2011 - Applied code generation in .NET
NetWork - 15.10.2011 - Applied code generation in .NET
 
Crafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an ArchitectCrafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an Architect
 
Clean Code Part III - Craftsmanship at SoCal Code Camp
Clean Code Part III - Craftsmanship at SoCal Code CampClean Code Part III - Craftsmanship at SoCal Code Camp
Clean Code Part III - Craftsmanship at SoCal Code Camp
 
Binary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code Testing
 
Old code doesn't stink
Old code doesn't stinkOld code doesn't stink
Old code doesn't stink
 
10 Reasons You MUST Consider Pattern-Aware Programming
10 Reasons You MUST Consider Pattern-Aware Programming10 Reasons You MUST Consider Pattern-Aware Programming
10 Reasons You MUST Consider Pattern-Aware Programming
 
Code quality
Code qualityCode quality
Code quality
 
NET Code Testing
NET Code TestingNET Code Testing
NET Code Testing
 
Enterprise Node - Code Quality
Enterprise Node - Code QualityEnterprise Node - Code Quality
Enterprise Node - Code Quality
 
CBDW2014 - Behavior Driven Development with TestBox
CBDW2014 - Behavior Driven Development with TestBoxCBDW2014 - Behavior Driven Development with TestBox
CBDW2014 - Behavior Driven Development with TestBox
 
Coding Naked
Coding NakedCoding Naked
Coding Naked
 
A Tale of Two Pizzas: Accelerating Software Delivery with AWS Developer Tools
A Tale of Two Pizzas: Accelerating Software Delivery with AWS Developer ToolsA Tale of Two Pizzas: Accelerating Software Delivery with AWS Developer Tools
A Tale of Two Pizzas: Accelerating Software Delivery with AWS Developer Tools
 
Enter the mind of an Agile Developer
Enter the mind of an Agile DeveloperEnter the mind of an Agile Developer
Enter the mind of an Agile Developer
 
ENT201 A Tale of Two Pizzas: Accelerating Software Delivery with AWS Develope...
ENT201 A Tale of Two Pizzas: Accelerating Software Delivery with AWS Develope...ENT201 A Tale of Two Pizzas: Accelerating Software Delivery with AWS Develope...
ENT201 A Tale of Two Pizzas: Accelerating Software Delivery with AWS Develope...
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 
Autoframework design
Autoframework designAutoframework design
Autoframework design
 
Writing less code with Serverless on AWS at AWS User Group Nairobi
Writing less code with Serverless on AWS at AWS User Group NairobiWriting less code with Serverless on AWS at AWS User Group Nairobi
Writing less code with Serverless on AWS at AWS User Group Nairobi
 
AspectMock
AspectMockAspectMock
AspectMock
 
Improving the Quality of Existing Software - DevIntersection April 2016
Improving the Quality of Existing Software - DevIntersection April 2016Improving the Quality of Existing Software - DevIntersection April 2016
Improving the Quality of Existing Software - DevIntersection April 2016
 
Wavect - Your long-term partner for sophisticated and maintainable software
Wavect - Your long-term partner for sophisticated and maintainable softwareWavect - Your long-term partner for sophisticated and maintainable software
Wavect - Your long-term partner for sophisticated and maintainable software
 

More from Eran Stiller

Architecting at Scale with the Advice Process
Architecting at Scale with the Advice ProcessArchitecting at Scale with the Advice Process
Architecting at Scale with the Advice Process
Eran Stiller
 
Application Evolution Strategy
Application Evolution StrategyApplication Evolution Strategy
Application Evolution Strategy
Eran Stiller
 
Developing and Deploying Microservices with Project Tye
Developing and Deploying Microservices with Project TyeDeveloping and Deploying Microservices with Project Tye
Developing and Deploying Microservices with Project Tye
Eran Stiller
 
API Design in the Modern Era - Architecture Next 2020
API Design in the Modern Era - Architecture Next 2020API Design in the Modern Era - Architecture Next 2020
API Design in the Modern Era - Architecture Next 2020
Eran Stiller
 
Bot-Tender: A Chat Bot Walks into a Bar (2020)
Bot-Tender: A Chat Bot Walks into a Bar (2020)Bot-Tender: A Chat Bot Walks into a Bar (2020)
Bot-Tender: A Chat Bot Walks into a Bar (2020)
Eran Stiller
 
Why Don’t You Understand Me? Build Intelligence into Your Apps
Why Don’t You Understand Me? Build Intelligence into Your AppsWhy Don’t You Understand Me? Build Intelligence into Your Apps
Why Don’t You Understand Me? Build Intelligence into Your Apps
Eran Stiller
 
Modern Microservices Architecture with Docker
Modern Microservices Architecture with DockerModern Microservices Architecture with Docker
Modern Microservices Architecture with Docker
Eran Stiller
 
Windows Containers - Microsoft Ignite The Tour
Windows Containers - Microsoft Ignite The TourWindows Containers - Microsoft Ignite The Tour
Windows Containers - Microsoft Ignite The Tour
Eran Stiller
 
Architecting Multitenant SaaS Applications with Azure - Microsoft Ignite The ...
Architecting Multitenant SaaS Applications with Azure - Microsoft Ignite The ...Architecting Multitenant SaaS Applications with Azure - Microsoft Ignite The ...
Architecting Multitenant SaaS Applications with Azure - Microsoft Ignite The ...
Eran Stiller
 
Bot Framework - Microsoft Ignite The Tour
Bot Framework - Microsoft Ignite The TourBot Framework - Microsoft Ignite The Tour
Bot Framework - Microsoft Ignite The Tour
Eran Stiller
 
It's a Serverless World
It's a Serverless WorldIt's a Serverless World
It's a Serverless World
Eran Stiller
 
Keynote - From Monolith to Microservices - Lessons Learned in the Real World
Keynote - From Monolith to Microservices - Lessons Learned in the Real WorldKeynote - From Monolith to Microservices - Lessons Learned in the Real World
Keynote - From Monolith to Microservices - Lessons Learned in the Real World
Eran Stiller
 
Architecting a Serverless IoT System in the Cloud
Architecting a Serverless IoT System in the CloudArchitecting a Serverless IoT System in the Cloud
Architecting a Serverless IoT System in the Cloud
Eran Stiller
 
6 Lessons I Learned on my Journey from Monolith to Microservices
6 Lessons I Learned on my Journey from Monolith to Microservices6 Lessons I Learned on my Journey from Monolith to Microservices
6 Lessons I Learned on my Journey from Monolith to Microservices
Eran Stiller
 
IoT in Action Keynote - CodeValue
IoT in Action Keynote - CodeValueIoT in Action Keynote - CodeValue
IoT in Action Keynote - CodeValue
Eran Stiller
 
Net Conf Israel - Intro & Building Cloud Native Apps with .NET Core 3.0 and K...
Net Conf Israel - Intro & Building Cloud Native Apps with .NET Core 3.0 and K...Net Conf Israel - Intro & Building Cloud Native Apps with .NET Core 3.0 and K...
Net Conf Israel - Intro & Building Cloud Native Apps with .NET Core 3.0 and K...
Eran Stiller
 
Create Your Own Serverless PKI with .NET & Azure Key Vault
Create Your Own Serverless PKI with .NET & Azure Key VaultCreate Your Own Serverless PKI with .NET & Azure Key Vault
Create Your Own Serverless PKI with .NET & Azure Key Vault
Eran Stiller
 
Cloud Native Development on Azure
Cloud Native Development on AzureCloud Native Development on Azure
Cloud Native Development on Azure
Eran Stiller
 
Today, the Cloud Is Your Advantage
Today, the Cloud Is Your AdvantageToday, the Cloud Is Your Advantage
Today, the Cloud Is Your Advantage
Eran Stiller
 
Build 2019 Recap
Build 2019 RecapBuild 2019 Recap
Build 2019 Recap
Eran Stiller
 

More from Eran Stiller (20)

Architecting at Scale with the Advice Process
Architecting at Scale with the Advice ProcessArchitecting at Scale with the Advice Process
Architecting at Scale with the Advice Process
 
Application Evolution Strategy
Application Evolution StrategyApplication Evolution Strategy
Application Evolution Strategy
 
Developing and Deploying Microservices with Project Tye
Developing and Deploying Microservices with Project TyeDeveloping and Deploying Microservices with Project Tye
Developing and Deploying Microservices with Project Tye
 
API Design in the Modern Era - Architecture Next 2020
API Design in the Modern Era - Architecture Next 2020API Design in the Modern Era - Architecture Next 2020
API Design in the Modern Era - Architecture Next 2020
 
Bot-Tender: A Chat Bot Walks into a Bar (2020)
Bot-Tender: A Chat Bot Walks into a Bar (2020)Bot-Tender: A Chat Bot Walks into a Bar (2020)
Bot-Tender: A Chat Bot Walks into a Bar (2020)
 
Why Don’t You Understand Me? Build Intelligence into Your Apps
Why Don’t You Understand Me? Build Intelligence into Your AppsWhy Don’t You Understand Me? Build Intelligence into Your Apps
Why Don’t You Understand Me? Build Intelligence into Your Apps
 
Modern Microservices Architecture with Docker
Modern Microservices Architecture with DockerModern Microservices Architecture with Docker
Modern Microservices Architecture with Docker
 
Windows Containers - Microsoft Ignite The Tour
Windows Containers - Microsoft Ignite The TourWindows Containers - Microsoft Ignite The Tour
Windows Containers - Microsoft Ignite The Tour
 
Architecting Multitenant SaaS Applications with Azure - Microsoft Ignite The ...
Architecting Multitenant SaaS Applications with Azure - Microsoft Ignite The ...Architecting Multitenant SaaS Applications with Azure - Microsoft Ignite The ...
Architecting Multitenant SaaS Applications with Azure - Microsoft Ignite The ...
 
Bot Framework - Microsoft Ignite The Tour
Bot Framework - Microsoft Ignite The TourBot Framework - Microsoft Ignite The Tour
Bot Framework - Microsoft Ignite The Tour
 
It's a Serverless World
It's a Serverless WorldIt's a Serverless World
It's a Serverless World
 
Keynote - From Monolith to Microservices - Lessons Learned in the Real World
Keynote - From Monolith to Microservices - Lessons Learned in the Real WorldKeynote - From Monolith to Microservices - Lessons Learned in the Real World
Keynote - From Monolith to Microservices - Lessons Learned in the Real World
 
Architecting a Serverless IoT System in the Cloud
Architecting a Serverless IoT System in the CloudArchitecting a Serverless IoT System in the Cloud
Architecting a Serverless IoT System in the Cloud
 
6 Lessons I Learned on my Journey from Monolith to Microservices
6 Lessons I Learned on my Journey from Monolith to Microservices6 Lessons I Learned on my Journey from Monolith to Microservices
6 Lessons I Learned on my Journey from Monolith to Microservices
 
IoT in Action Keynote - CodeValue
IoT in Action Keynote - CodeValueIoT in Action Keynote - CodeValue
IoT in Action Keynote - CodeValue
 
Net Conf Israel - Intro & Building Cloud Native Apps with .NET Core 3.0 and K...
Net Conf Israel - Intro & Building Cloud Native Apps with .NET Core 3.0 and K...Net Conf Israel - Intro & Building Cloud Native Apps with .NET Core 3.0 and K...
Net Conf Israel - Intro & Building Cloud Native Apps with .NET Core 3.0 and K...
 
Create Your Own Serverless PKI with .NET & Azure Key Vault
Create Your Own Serverless PKI with .NET & Azure Key VaultCreate Your Own Serverless PKI with .NET & Azure Key Vault
Create Your Own Serverless PKI with .NET & Azure Key Vault
 
Cloud Native Development on Azure
Cloud Native Development on AzureCloud Native Development on Azure
Cloud Native Development on Azure
 
Today, the Cloud Is Your Advantage
Today, the Cloud Is Your AdvantageToday, the Cloud Is Your Advantage
Today, the Cloud Is Your Advantage
 
Build 2019 Recap
Build 2019 RecapBuild 2019 Recap
Build 2019 Recap
 

Recently uploaded

Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
Safe Software
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
Edge AI and Vision Alliance
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
Fwdays
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
ScyllaDB
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
operationspcvita
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
c5vrf27qcz
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
Ajin Abraham
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
Pablo Gómez Abajo
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
Alex Pruden
 
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Pitangent Analytics & Technology Solutions Pvt. Ltd
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 

Recently uploaded (20)

Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
 
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 

Enhance Your Code Quality with Code Contracts

  • 1. Code Contracts Enhance Your Code Quality with Code Contracts Eran Stiller Senior Software Architect, Founder March 20, 2013
  • 2. About Me • Software architect, consultant and instructor • Technology addict • 10 years of experience • .NET and Native Windows Programming • Specializes in large-scale, server-side, highly-concurrent systems • Co-Founder at CodeValue Ltd. • http://stiller.co.il/blog
  • 3. Agenda • Back to Basics • Design by Contract • Code Contracts – Preconditions – Postconditions – Invariants • Internals • External Tools
  • 5. The If…Throw Approach public class MyClass { public void SomeMethod(string s, int i) { if (s == null) throw new ArgumentNullException( "s", "Your input string cannot be null!"); if (i < 0) throw new ArgumentException( "Your input must be non-negative!", "i"); // Your code here } }
  • 6. The If…Throw Approach • Issues – Cumbersome – lots of code – Indistinguishable from method code – Does not apply to interfaces – Does not apply across object inheritance – Not visible at call site – Not part of the contract
  • 7. Design by Contract “Design by contract (DbC) (…) is an approach for designing software. It prescribes that software designers should define formal, precise and verifiable interface specifications for software components, which extend the ordinary definition of abstract data types with preconditions, postconditions and invariants. These specifications are referred to as "contracts", in accordance with a conceptual metaphor with the conditions and obligations of business contracts.” (Wikipedia)
  • 8. Enter Code Contracts • Microsoft’s Design-by-Contract implementation for .NET – A subset of Spec# • Language-agnostic way to express coding assumptions • Checked documentation of your API – Improved testability – Static verification – API documentation
  • 9. Requirements • .NET 4.0/4.5 • Visual Studio 2010/2012 – Any edition except “Express” edition • Download and install from Microsoft Research – http://research.microsoft.com/en-us/projects/contracts/ • Allowed for commercial use • In addition – Support exists for .NET 3.5 and VS 2008 as well – Contracts API is part of an external assembly
  • 10. Preconditions • Define what your API expects – Parameter validation – Object state – Environment condition – Anything the caller can check!
  • 11. Postconditions • Define what the caller can expect from your API – Return value constraints – Exception constraints – Object state after call
  • 12. Invariants • Define the rules which govern your object’s behavior – Object state – Field & property values – Things that always hold true
  • 13. Enough Talking. Show Me the Code!
  • 14. Should I Use It? It Depends… • Pros – Reduce bugs – Static verification – API documentation – Improved testability – Shallow learning curve • Cons – Experimental project – Static verifier less than perfect – Some issues with VS2012 – Compilation time increase
  • 15. There is a Middle Way! public class MyClass { public void SomeMethod(string s, int i) { if (s == null) throw new ArgumentNullException( "s", "Your input string cannot be null!"); if (i < 0) throw new ArgumentException( "Your input must be non-negative!", "i"); Contract.EndContractBlock(); // Your code here } }
  • 16. How Can I Choose? From the User Manual
  • 17. Code Contracts Internals • Contract Rewriter – ccrewrite • Static Verifier – cccheck • API Documentation – ccdoc • Ships with contract assemblies for many BCL types & methods
  • 19. Code Contracts Editor Extension • Visual Studio 2010 Only – 2012 support still pending – http://tinyurl.com/CCEditorExtensions • Has some stability issues
  • 20. Summary • A contract is more than an Interface • Design by Contract is a design approach • Code Contracts enables Design by Contract on .NET – Preconditions, Postconditions & Invariants • An experimental project from Microsoft Research • Rewrites your application’s IL code • Integrates with Editor Extensions & Pex • Give it a try!
  • 21. Resources • My Blog – http://stiller.co.il/blog • Code Contracts – http://research.microsoft.com/en-us/projects/contracts/ – http://research.microsoft.com/en-us/projects/contracts/userdoc.pdf – http://social.msdn.microsoft.com/Forums/en-US/codecontracts/threads • Design by Contract – http://en.wikipedia.org/wiki/Design_by_contract • Pex – http://research.microsoft.com/en-us/projects/pex/
  • 22. Presenter contact details c: +972-54-6160646 e: erans@codevalue.net b: stiller.co.il/blog w: www.codevalue.net

Editor's Notes

  1. Ask the crowd what do they think is the contract
  2. Coined by Bertrand Meyer – the creator of Eiffel