CSC/ECE 517 Fall 2011/ch6 6a pc
6a (was 5c). Lecture 18, Programming by contract. My notes for Lecture 18 are largely taken from 20-year-old articles by Bertrand Meyer. The principles are timeless, but there are undoubtedly new embellishments that would provide better background to the topics discussed in the lecture. Write a narrative, generally following the lecture organization, that explains programming by contract in more detail.
Introduction
Software Reliability
A major concern in software design is Reliability, which depends on two factors: 1) Correctness - system's ability to perform its job according to the specification and 2) Robustness - handling abnormal conditions. Reliability is especially important in Object oriented software because of the additional complexity imposed by Reusability. Some of the common problems encountered when writing software include:
- System failure - CPU crash, Disk/Memory access errors, Network errors
- Invalid input data - Out of range data, Bad file name,etc.
- Programming errors - Memory leaks, Buffer overruns, etc.
There are many programming language features that help tackle these issues. Static typing, for example, is a major help for catching inconsistencies before they have had time to become bugs. Similarly Garbage collection helps to remove the specter of devious memory management errors. Various software design methodologies also come handy when dealing with complex software. For instance, reusability itself can help eliminate lots of bugs, if we are reusing existing code that has already been thoroughly tested and deployed. Polymorphism also helps in handling issues related to maintainability by reducing the size of the code and making it more elegant. But in a large and complex software involving multiple developers, we need a more systematic approach of specifying and implementing object-oriented software elements and their interactions. This is exactly what "Programming by contract" promises to do.
Programming by contract
Basically programming by contract creates a contract between the software developer and software user, which are referred to as the supplier and the consumer. Every feature, or method, starts with a precondition that must be satisfied by the consumer of the routine. And each feature ends with postconditions which the supplier guarantees to be true (if and only if the preconditions were met). For example:
The presence of a specification, even if it does not fully guarantee the module's correctness, is a good basis for systematic testing and debugging.
The Design by Contract theory, then, suggests associating a specification with every software element. These specifications (or contracts) govern the interaction of the element with the rest of the world. This is different from having a formal specification in that the specification language is embedded in the design and programming language (e.g. Eiffel).
Contract and Inheritance
Class invariant
Terminology
Preconditions
Postconditions
Invariants
Assertions
Reusability
Comparison with Defensive Programming
Advantages of Programming by Contract
When not to use Programming by Contract
Asserts and "by contract" specifications catch programmer errors not run-time errors! (Add more details)