SlideShare a Scribd company logo
1 of 26
Download to read offline
DESIGN BY CONTRACT
IN RUBY
by Alexey Fedorov
CLASSICAL DEFENSIVE PROGRAMMING
def add(a, b)
  a + b
end
def add(a, b)
  raise "a should be Fixnum or Float" unless a.is_a?(Fixnum) ||
    a.is_a?(Float)
  raise "b should be Fixnum or Float" unless b.is_a?(Fixnum) ||
    b.is_a?(Float)
  result = a + b
  raise "result should be Fixnum or Float" unless result.is_a?(Fixnum) ||
    result.is_a?(Float)
  result
end
GEM "CONTRACTS"
Contract Num, Num => Num
def add(a, b)
  a + b
end
DESIGN BY CONTRACT
Answers following questions:
What does it expect?
What does it guarantee?
What does it maintain?
BENEFITS
Clients can be confident using its public APIs
Service itself can be confident in its own operations
ASSERTON STEROIDS. AND IT IS NOT ONLY
ABOUT TYPES
Contract ActiveUser => Rating
def rating_for(active_user)
  # calculate rating for active user
end
class ActiveUser
  def self.valid?(user)
    # check that user is active
  end
end
VERY USEFUL CONTRACT VIOLATION ERROR
MESSAGES
ContractError: Contract violation for argument 1 of 1:
    Expected: ActiveUser,
    Actual: #<User:0x00000101059540> {last_activity=27.11.2014}
    Value guarded in: Object::rating_for
    With Contract: ActiveUser => Rating
    At: (irb):10
    ... backtrace ...
PATTERN MATCHING, SORTA..
Contract 1 => 1
def factorial(_)
  1
end
Contract Num => Num
def factorial(number)
  number * factorial(number ­ 1)
end
SOMETHING USEFUL
# Handles successful responses
Contract 200, JsonString => JsonString
def handle_response(status, response)
  transform_response(response)
end
# Handles failures by forwarding them
Contract Num, String => String
def handle_response(status, response)
  response
end
LIMITLESS BENEFITS
Consistency of input
Consistency of data flows inside the system
Consistency of state of the system
Consistency of output
Blow up loudly on any logical error in the system
CAVEATS
Performance?
PERFORMANCE
Benchmark Slowdown
a+b 900% slowdown
production system with network IO 5-10% slowdown
NO_CONTRACTS=1 0% slowdown
No loud blowing up with NO_CONTRACTS=1in production
LINKS
Github: github.com/egonSchiele/contracts.ruby
Nice tutorial: egonschiele.github.io/contracts.ruby
Creator: github.com/egonSchiele
Me, co-maintainer: github.com/waterlink
THANKS
Q & A

More Related Content

Similar to Design by Contract in Ruby with the Contracts Gem

Salesforce B2B Solution Architect | Questions & Answers
Salesforce B2B Solution Architect | Questions & AnswersSalesforce B2B Solution Architect | Questions & Answers
Salesforce B2B Solution Architect | Questions & AnswersAdinaCoyle
 
Application Programming Interface Implementation For Building Software Applic...
Application Programming Interface Implementation For Building Software Applic...Application Programming Interface Implementation For Building Software Applic...
Application Programming Interface Implementation For Building Software Applic...SlideTeam
 
Neha Gupta - AIR Mobile: Cross promotion
Neha Gupta - AIR Mobile: Cross promotionNeha Gupta - AIR Mobile: Cross promotion
Neha Gupta - AIR Mobile: Cross promotionFlash Conference
 
Best practices for upgrading vb 6.0 projects to vb.net
Best practices for upgrading vb 6.0 projects to vb.netBest practices for upgrading vb 6.0 projects to vb.net
Best practices for upgrading vb 6.0 projects to vb.netajmal_fuuast
 
C_ARCON_2202 Dumps Questions
C_ARCON_2202 Dumps QuestionsC_ARCON_2202 Dumps Questions
C_ARCON_2202 Dumps QuestionsStudy Material
 
How to build quality software
How to build quality softwareHow to build quality software
How to build quality softwareMake School
 
Edition of an enterprise software in PHP, feedback
Edition of an enterprise software in PHP, feedbackEdition of an enterprise software in PHP, feedback
Edition of an enterprise software in PHP, feedbackNicolas Dupont
 
ITIL_v4_Foundation_May_2022-v1.1.pdf
ITIL_v4_Foundation_May_2022-v1.1.pdfITIL_v4_Foundation_May_2022-v1.1.pdf
ITIL_v4_Foundation_May_2022-v1.1.pdfCCIEHOMER
 
Biz Talk Server Certification
Biz Talk Server CertificationBiz Talk Server Certification
Biz Talk Server CertificationVskills
 
Guice tutorial
Guice tutorialGuice tutorial
Guice tutorialAnh Quân
 
Srecon18americas lightning talk: Auto-Cascading Security Updates Through Dock...
Srecon18americas lightning talk: Auto-Cascading Security Updates Through Dock...Srecon18americas lightning talk: Auto-Cascading Security Updates Through Dock...
Srecon18americas lightning talk: Auto-Cascading Security Updates Through Dock...Andrey Falko
 
Week 15 learning objectives
Week 15 learning objectivesWeek 15 learning objectives
Week 15 learning objectivesYap Hooi
 
API Integration For Building Software Applications Powerpoint Presentation Sl...
API Integration For Building Software Applications Powerpoint Presentation Sl...API Integration For Building Software Applications Powerpoint Presentation Sl...
API Integration For Building Software Applications Powerpoint Presentation Sl...SlideTeam
 

Similar to Design by Contract in Ruby with the Contracts Gem (20)

4 h0 020
4 h0 0204 h0 020
4 h0 020
 
Salesforce B2B Solution Architect | Questions & Answers
Salesforce B2B Solution Architect | Questions & AnswersSalesforce B2B Solution Architect | Questions & Answers
Salesforce B2B Solution Architect | Questions & Answers
 
Application Programming Interface Implementation For Building Software Applic...
Application Programming Interface Implementation For Building Software Applic...Application Programming Interface Implementation For Building Software Applic...
Application Programming Interface Implementation For Building Software Applic...
 
Vcaw5102013
Vcaw5102013Vcaw5102013
Vcaw5102013
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
C++
C++C++
C++
 
Neha Gupta - AIR Mobile: Cross promotion
Neha Gupta - AIR Mobile: Cross promotionNeha Gupta - AIR Mobile: Cross promotion
Neha Gupta - AIR Mobile: Cross promotion
 
Best practices for upgrading vb 6.0 projects to vb.net
Best practices for upgrading vb 6.0 projects to vb.netBest practices for upgrading vb 6.0 projects to vb.net
Best practices for upgrading vb 6.0 projects to vb.net
 
C_ARCON_2202 Dumps Questions
C_ARCON_2202 Dumps QuestionsC_ARCON_2202 Dumps Questions
C_ARCON_2202 Dumps Questions
 
C taw12 70
C taw12 70C taw12 70
C taw12 70
 
How to build quality software
How to build quality softwareHow to build quality software
How to build quality software
 
2V0-51-21 Exam Dumps
2V0-51-21 Exam Dumps2V0-51-21 Exam Dumps
2V0-51-21 Exam Dumps
 
Edition of an enterprise software in PHP, feedback
Edition of an enterprise software in PHP, feedbackEdition of an enterprise software in PHP, feedback
Edition of an enterprise software in PHP, feedback
 
ITIL_v4_Foundation_May_2022-v1.1.pdf
ITIL_v4_Foundation_May_2022-v1.1.pdfITIL_v4_Foundation_May_2022-v1.1.pdf
ITIL_v4_Foundation_May_2022-v1.1.pdf
 
Biz Talk Server Certification
Biz Talk Server CertificationBiz Talk Server Certification
Biz Talk Server Certification
 
Google GIN
Google GINGoogle GIN
Google GIN
 
Guice tutorial
Guice tutorialGuice tutorial
Guice tutorial
 
Srecon18americas lightning talk: Auto-Cascading Security Updates Through Dock...
Srecon18americas lightning talk: Auto-Cascading Security Updates Through Dock...Srecon18americas lightning talk: Auto-Cascading Security Updates Through Dock...
Srecon18americas lightning talk: Auto-Cascading Security Updates Through Dock...
 
Week 15 learning objectives
Week 15 learning objectivesWeek 15 learning objectives
Week 15 learning objectives
 
API Integration For Building Software Applications Powerpoint Presentation Sl...
API Integration For Building Software Applications Powerpoint Presentation Sl...API Integration For Building Software Applications Powerpoint Presentation Sl...
API Integration For Building Software Applications Powerpoint Presentation Sl...
 

Recently uploaded

Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 

Recently uploaded (20)

Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 

Design by Contract in Ruby with the Contracts Gem