Design By Contract (DBC)


Hair Dryer Example | DBC Concepts | Why DBC Is Used

Engagement Example | Chemical Tank Example | Class Exercise

Return To Home Page


Hair Dryer Example

Scenario:

A student is going on London Study. He chooses to bring his own hair dryer with a plug adapter so it will fit in the different socket.
Question: What will happen if the student plugs in the hair dryer?
Answer: 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

DBC Concepts

The Contract:
Preconditions: (The other party's rights and responsibilities.)
Postconditions: (Your rights and responsibilities.)
Failure to meet the contract = bug.

Write "Lazy" Code
***DO NOT error check internally, but DO error check against the world.***
Back to Top of Page

Why DBC Is Used

Assertions and Crashing Early

Assertions are an option to check for problems during the debugging phase in DBC. 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.

For a sublesson on these topics click
here.

Code Samples

Here are code samples that illustrate why DBC is a better method than exception handling.
Two programs, one in C++ and one in JAVA, were written to read in an integer. If a string is passed rather than an integer, what will each program print?

SampleCode.pdf

C++ Code Sample

C++ gives you a choice between DBC and exceptions. (We will not use exceptions for our experiment).

#include < iostream.h > // Compile with -Wno-deprecated when using < iostream.h >
int main( ){
    int x;
    cout << "Input an integer: ";
    cin >> x;
    cout << "Your integer is " << x << endl;
    return 0;
}

Input: "Hello" or Hello (or any string)
Output: Garbage

JAVA Code Sample

JAVA has exception handling built into it.

public class ReadInt {
    public static void main(String [] args) {
       int x;
       String str = new String(args[0]);
       try {
          x = Integer.parseInt(str);
          System.out.println("Your integer is " + x);
       }
       catch (NumberFormatException e) {
          System.out.println("NumberFormatException Thrown");
          return;
       }
       return;
    }
}

Input: "Hello" or Hello (or any string)
Output: NumberFormatException Thrown

Why is DBC a better choice?

Back to Top of Page

Engagement Example

Scenario:

This is the contract for a man to get engaged to a woman. A woman (the caller) may enter a relationship with the man (the method) only if she satisfies the qualities he is looking for in a woman (preconditions). If there are no unforeseen obstacles (exceptions), then they will get engaged (postcondition).

Preconditions:

Postcondition:

Exceptions:

Back to Top of Page

Chemical Tank Example

A class called ChemicalTank has five methods: isTankFull, turnOffBottomValve, turnOffTopValve, turnOnBottomValve, turnOnTopValve.

The first Javadoc in the link below shows ChemicalTank using design by contract mentality.   Design by contract has two rivals: error return and exception handling.   Two other classes, ChemicalTankWithErrorReturn and ChemicalTankWithExceptions, were written to show the differences between design by contract, error return, and exception handling.

To view the three classes written for the chemical tank click
here.
Back to Top of Page

Group Exercises

Screening For A New Roommate

Scenario:

You (the caller), as a potential roommate, must make sure you meet the criteria (preconditions) necessary to qualify as a roommate at the prospective house. If you have these qualities, you wait while the screeners make their decision (the method) and choose you as their roommate (postcondition).

Example Preconditions:

Example Postcondition:

Parent/Child Relationship - Child Going to a Party

Scenario:

You as the child (the caller) must provide sufficient information (preconditions) about the party you are going to. Once your parents have discussed and are satisfied with the information you have given them about the party (the method), you will be able to go to the party (postcondition).

Example Preconditions:

Example Postcondition:

Back to Top of Page