Run-time (e.g. insufficient memory, non-existent file) errors are outside the contract and must be checked and handled normally.
The design issue is easy to state but often difficult to determine.
Which conditions are trusted and which are checked?
Design By Contract means:
A simple example is adapted from Writing Solid Code published
by Microsoft Press. Design By Contract is a major tenet in this
widely adopted book.
Convert to upper case (Non - DBC)
Convert to upper case (DBC)
Notes:
1. This idea can be quite difficult to sell; many developers have
a mistaken "bullet-proofing" mentality.
2. Think about code that does check preconditions? How should a
precondition violation be handled?
Comparison: DBC and traditional error-checking practice
|   | at Development time | at Run-time | Notes |
|---|---|---|---|
| Error-checking |
The method checks (used) data members and its parameters. In case of an error, the method triggers an exception or returns an error. |
The method code runs as written by the programmer. Product quality code must handle any "conceivable" error. |
In any case, such code must be written for run-time errors (errors caused
by the operating environment). A long discussion on "conceivable" is required for all software projects. |
| Design by Contract | The designer writes strong preconditions against data members and
input parameters. Code to check the preconditions may be:
|
If there is no run-time error checking code, the method runs with defects
in its input; results are indeterminate.
The method may work correctly or produce a defect or crash the program or ?? If there is run-time error checking code, that code does something appropriate. It must be possible to disable the run-time code; this may be done at build-time or by the user depending on circumstances. The code might:
|
Meyer defines 3 possible outcomes in an Eiffel rescue clause.
|