Fun with Design By Contract (DBC)


Mowing Lawns Example | DBC Concepts | Why DBC Is Used

Examples  | Chemical Tank Example 


Mowing Lawns Example

Scenario:

You (the supplier), offer to mow lawns for people.  You don't want to take on any unrealistic jobs that would be impossible to complete. So you specify criteria (preconditions) to potential customers (the client) about restrictions on the kind of lawns you will mow.  If customer's lawn meets the criteria, you will  provide a mowed lawn (postcondition).
.

Example Preconditions:

        Example Postcondition:

Example Exceptions


DBC Concepts

DBC is a technique that addresses the design of interfaces between modules in a software system.  It aims to reduce defects, improve reliability, and reduce development costs.
The Contract:
Preconditions: (The client's responsibilities.)
Postconditions: (The supplier's responsibilities.)
Exceptions: ("Outside world" events that would cause the postconditions to not be met.)

Failure to meet the contract (by either party) is a defect.

DBC authors write "Lazy" code
***DO NOT error check internally, but DO error check against the world.***

Diagram of scope of contracts showing events outside system boundary can't be subject to DBC.
Back to Top of Page

Why DBC Is Used

Assertions and Crashing Early

Assertions are an option to check for DBC problems during the testing phase. Their default is to crash the program at the site where the assertion fails. This is a good method to catch a problem and fix it where it originates rather than after it has infected the rest of the program.

In order for DBC to be kept in its purest form, the assertions should be turned off before shipping the product to the customer.

View a sublesson on this toic.

Code Samples

Here are code samples that illustrate why DBC is a better method than exception handling. Two programs were written to read in a string and convert it to upper case. If a string is passed containing numeric digits rather than letters, what will each program print?
Convert to upper case (Non - DBC)

Convert to upper case (DBC)

Why is DBC a better choice?

Back to Top of Page


Examples


Shoveling Snow

Scenario:

I (the supplier) offer to shovel someone's driveway and sidewalk.  I want to be sure the job is manageable and I get paid in cash. Customers want a clean, safe sidewalk to walk on.  I specify the criteria (preconditions) necessary for me to shovel snow.  Customers who agree to the requirements (preconditions) receive a sidewalk free of snow.

Preconditions:

Postconditions:

Exception:

It starts snowing after service has started, some surfaces will have fresh snow on them.


Car Mechanic

 Scenario:

The customer (the client) must agree to the requirements (preconditions) about leaving their car for  service. You the mechanic (the supplier)  deliver a serviced car in good working order (postcondition).

Example Preconditions:

Example Postcondition:

Back to Top of Page


Hair Dryer Example

Scenario:

A student is going on London Study. She chooses to bring her own hair dryer with a plug adapter so it will fit in the different sockets that are used in the U.K..
Question: What will happen if the student plugs in the hair dryer?
  1. It will work correctly.
  2. It will blow up.
  3. It will trip a circuit breaker.
  4. The results are unpredictable.
Answer: 4. According to DBC, we aren't sure what will happen. It might work correctly. It might blow up. It might blow a circuit breaker.
Think about why this is the case and why the hair dryer was designed this way.

US Hair Dryer

Preconditions:

  • The hair dryer will be plugged into a 110 volt power source.
  • The two or three thin prongs of the plug fit into the socket (or adapter).

Postconditions:

  • The hair dryer works correctly.

UK Hair Dryer

Preconditions:

  • The hair dryer will be plugged into a 220 volt power source.
  • The three thick prongs fit into the socket (or adapter).

Postconditions:

  • The hair dryer works correctly.
Back to Top of Page

Chemical Tank - Software Example

A class called ChemicalTank represents a tank full of liquid chemicals.  It has five methods: isTankFull, turnOffBottomValve, turnOffTopValve, turnOnBottomValve, turnOnTopValve.
 
ChemicalTank using DBC show the class designed using DBC principles.

Design By Contract has two alternate approaches: error return and exception handling.  
ChemicalTankWithErrorReturn
ChemicalTankWithExceptions

Back to Top of Page

This paper is modified from the original paper by Amy Rideg.