Example Preconditions:
- The lawn must be shorter than 6 inches high.
- The lawn must be dry.
- The lawn must be free of leaves.
- The ground must be level.
- There must be fewer than 5 dog turds per 1000 square feet.
- There must be no unleashed dogs in the vicinity. (Safety concern)
- The lawn is mowed to a height specified by the customer
- The lawn clippings are disposed of in the green barrel or compost pile.
Example Exceptions
- A sudden thunderstorm will cause the lawn to be left partially mowed.
The Contract:
Preconditions: (The client's responsibilities.)
- Has two participants, the supplier and the client, each who is willing to abide by certain restrictions in order to obtain certain benefits.
("The supplier" is provider of some service and "the client" is the user or consumer of that service. In software systems, the supplier is usually a low-level module and the client is a module which invokes it.)- Defines the rights and responsibilities of both parties.
- Is an agreement to repercussions for breaking the contract.
Postconditions: (The supplier's responsibilities.)
- Are how the method specifies its requirements to run correctly.
- Must be true for the method to operate correctly.
- Relies on the caller to pass valid parameters.
Exceptions: ("Outside world" events that would cause the postconditions to not be met.)
- Are guaranteed by the supplier if the preconditions are met.
- Will conclude the method. (There are no infinite loops.)
- Must be explicitly specified.
- Are outside of the control of the client and therefore not part of the preconditions.
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.***
- Be strict in what you accept. (preconditions)
- Promise little in return. (postconditions)
Diagram of scope of contracts showing events outside system boundary can't be subject to DBC.
Convert to upper case (Non - DBC)
Convert to upper case (DBC)
Why is DBC a better choice?
- Compare code samples
- The DBC sample is much shorter.
- It was faster/easier to code the DBC sample. More time was spent debugging the non-DBC sample.
- It takes less time to compile a program without error handling.
- It takes less time to execute a program without error handling.
- How many times will the error handling be used in comparison to the time and money spent to put it in the code and test it?
- Only the client can adequately assess why the error occurred, so the supplier is not the place to handle the error.
- There will often be redundant checks in both client and supplier, requiring twice the amount of testing.
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:
- There must be no more than a foot of snow on the ground.
- It must not be snowing at starting time of service.
- You must request the service between the hours of 9 am and 4 pm.
- You must not have driven more than three times over any of the area you want shoveled.
- You must pay in cash.
Postconditions:
- The customer's driveway and sidewalk have been cleared of all snow.
Exception:
It starts snowing after service has started, some surfaces will have fresh snow on them.
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:
- You must provide the car by 9am
- You must leave the keys with the car.
- You may not leave any pets or children in the car.
- You must be willing to leave the car all day.
Example Postcondition:
- The serviced car is available for pickup after 4 pm.
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?
- It will work correctly.
- It will blow up.
- It will trip a circuit breaker.
- 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
|
UK Hair Dryer
|
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