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
|
UK Hair Dryer
|
The Contract:
Preconditions: (The other party's rights and responsibilities.)
- Defines your rights and responsibilities as well as those of the other party.
("You" are the method, and "the other party" is the caller of your method.)- Is an agreement to repercussions for breaking the contract.
Postconditions: (Your rights and responsibilities.)
- Are how the method specifies its requirements to run correctly.
- Must be true for the method to be called.
- Relies on the caller to pass valid parameters.
Failure to meet the contract = bug.
- Are guaranteed by the method if the preconditions are met.
- Will conclude the method.   (There are no infinite loops.)
Write "Lazy" Code
***DO NOT error check internally, but DO error check against the world.***
- Be strict in what you accept.   (preconditions)
- Promise little in return.   (postconditions)
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?
- Compare code samples (on overhead projector)
- The C++ sample is much shorter.
- It was faster/easier to code the C++ sample.   More time was spent debugging the JAVA sample.
- It takes less time to compile a program without exceptions.
- How many times will the exception be used in comparison to the time and money spent to put it in the code?
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:
- She is a girl.
- She is a virgin.
- She has never been married.
- She does not have STDs.
- She has no criminal record.
- She is not related to the man in question.
Postcondition:
- Engagement
Exceptions:
- She moves across the country.
- She cheats on him.
- She falls out of love with him.
- She beats him.
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.
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:
- You must be neat.
- You must not have a criminal record.
- You must be sociable.
- You must not do drugs.
Example Postcondition:
- You become the screeners' new roommate.
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:
- The party must not involve drugs or alcohol.
- The party must be chaperoned.
- The party must be in a good neighborhood.
- You must be home by 11:00pm.
- Your parents must know the party host.
- You must provide the phone number where the party is being held.
- You must provide the address where the party is being held.
Example Postcondition:
- Child goes to the party.