Kurt Nørmark ©
Department of Computer Science, Aalborg University, Denmark
Abstract Previous lecture Next lecture Index References Contents | This lecture is about program correctness relative to specifications given by assertions. We discuss preconditions, postconditions, and invariants. The use of assertions for specifications of object-oriented program is known as Design by Contract. |
Correctness |
Software Qualities Slide Annotated slide Contents Index References Textbook |
|
Table. Different program qualities listed by name, description, and (for selected qualities) a contrasting, opposite quality |
|
|
Correctness Slide Annotated slide Contents Index References Textbook |
|
|
Specifications Slide Annotated slide Contents Index References Textbook |
The concept program specification: A program specification is a definition of what a computer program is expected to do [Wikipedia]. What - not how. |
|
|
Program: An algebraic specification of a stack. |
|
Program: An axiomatic specification of the squareroot function. |
|
Specification with preconditions and postconditions |
Logical expressions Slide Annotated slide Contents Index References Textbook |
The concept logical expression: A logical expression is an expression of type boolean | ||
The concept assertion: An assertion is a logical expression, which, if false, indicates an error [Foldoc] | ||
The concept precondition: A precondition of an operation is an assertion which must be true just before the operation is called | ||
The concept postcondition: A postcondition of an operation is an assertion which must be true just after the operation has been completed |
|
|
Examples of preconditions and postconditions Slide Annotated slide Contents Index References Textbook |
|
A circular list. The large yellow object represents the circular list as such. The circular green nodes represent the elements of the list. The rectangular nodes are instances of a class akin to LinkedListNode, which connect the constituents of the list together. |
Program: Circular list with preconditions and postconditions. |
|
An Assertion Language Slide Annotated slide Contents Index References Textbook |
|
|
Responsibilities and Contracts |
Questions Related to Responsibility Slide Annotated slide Contents Index References |
|
Division of Responsibilities Slide Annotated slide Contents Index References Textbook |
|
|
|
The highly responsible program Slide Annotated slide Contents Index References Textbook |
|
Program: Excerpt of highly responsible class Client of BankAccount. |
|
Program: Excerpt of highly responsible class BankAccount. |
|
Responsibility division by pre and postconditions Slide Annotated slide Contents Index References Textbook |
|
|
|
Contracts Slide Annotated slide Contents Index References Textbook |
|
The concept contract: A contract expresses the mutual obligations in between parts of a program that cooperate about the solution of some problem |
|
Everyday Contracts Slide Annotated slide Contents Index References Textbook |
|
|
Contracts: Obligations and Benefits Slide Annotated slide Contents Index References Textbook |
Figure. A give-and-take situation involving a client and a server (supplier) class. |
|
Obligations and Benefits in Sqrt Slide Annotated slide Contents Index References Textbook |
Program: An axiomatic specification of the squareroot function. |
|
Table. A tabular presentation of the obligations and benefits of the squareroot function (in a server role) and its callers (in a client role). |
|
|
Class Invariants |
General aspects of contracts Slide Annotated slide Contents Index References Textbook |
|
The concept class invariant: A class invariant expresses properties of an object which are stable in between operations initiated via the public client interface |
|
Everyday invariants Slide Annotated slide Contents Index References Textbook |
|
|
An example of a class invariant Slide Annotated slide Contents Index References Textbook |
|
Program: Circular list with a class invariant. |
|
|
Inheritance is Subcontracting |
Inheritance and Contracts Slide Annotated slide Contents Index References Textbook |
|
Figure. The relationship between inheritance and contracts |
Subcontracting Slide Annotated slide Contents Index References Textbook |
|
|
|
Class invariants in the triangle class hierarchy Slide Annotated slide Contents Index References Textbook |
Figure. Most general triangle: 3 non-zero angles 3 non-zero edges Sum of angles: 180 degrees Isosceles triangle Equilateral triangle: Right triangle: Isosceles right triangle: |
|
Assertions in Abstract classes Slide Annotated slide Contents Index References Textbook |
|
Program: An abstract class with preconditions and postconditions. |
|
|
Design by Contract |
Design by Contract Slide Annotated slide Contents Index References Textbook |
|
|
DBC and Programming Languages Slide Annotated slide Contents Index References Textbook |
|
|
|
|
Code Contracts in C# 4.0 Slide Annotated slide Contents Index References |
|
|
Loop Invariants |
Loop Invariants Slide Annotated slide Contents Index References |
|
Program: Division by repeated subtraction. |
|
Program: Output of the division program. |
|
Program: Division by repeated subtraction - instrumented with output inside loop. |
|
Program: Output of the instrumented division program. |
|
Chapter 13: Contracts and Assertions
Course home Author home About producing this web Previous lecture (top) Next lecture (top) Previous lecture (bund) Next lecture (bund)
Generated: February 7, 2011, 12:22:05