Docsity

Design by Contract: Understanding Preconditions, Postconditions, and Assertions, Study notes of Computer Science

Partial preview of the text

Download Design by Contract: Understanding Preconditions, Postconditions, and Assertions and more Study notes Computer Science in PDF only on Docsity!

Design by Contract I

Dr. Roger T. Alexander University Services Building, Room 240 Voice: 491-7026, Fax: 491- [email protected] http://www.cs.colostate.edu/~rta

2

Topics

  • The nature of Contracts
  • Preconditions and Postconditions
  • Problems Expressing Contracts

3

Contracts

Contracts (in human affairs)

  • Contracts are written between two parties when one of them (the supplier) performs some task for the other (the client).
  • Each party expects some benefits from the contract, and accepts some obligations in return.
  • One party’s obligation is a benefit for the other.
  • The aim of the contract is to spell out these benefits and obligations.

4

Contract Example

Obligations Benefits Client (Must ensure precondition) Be at the Santa Barbara airport at least 5 minutes before scheduled departure time. Bring only acceptable baggage. Pay ticket price

(May benefit from postcondition) Reach Chicago

Supplier (Must ensure postcondition) Bring customer to Chicago

(May assume precondition) No need to carry passenger who is late, has unacceptable baggage, or has not paid ticket price

7

Assertions

  • A statement of something assumed to be

fact (i.e. a constraint).

  • E.g age of customer >= minimum drinking age.
  • An assertion is a property of some of the

values of program entities.

  • Can be used in the semantic specification of

routines.

8

Preconditions and Postconditions

  • Precondition expresses the properties that

must hold whenever a procedure is called.

  • Postcondition describes the properties that

the procedure guarantees when it returns.

  • Task performed by a procedure is by two

assertions, each corresponding to

precondition and postcondition.

9

Preconditions and Postconditions

  • Precondition binds clients.
    • Defines conditions under which a call procedure is legitimate.
  • Postcondition binds the server
    • Defines the conditions that must be ensured by procedure on return.
  • Benefit for client is that certain results will

be obtained after the call

10

Preconditions and Postconditions

  • Benefits for server is guarantee that certain

assumptions will be satisfied whenever the

procedure is called.

  • If the client’s part of the contract is not

fulfilled (if the call does not satisfy the

precondition), then the class is not bound by

the postcondition.

13

Who should do the checking?

  • Observation: A main source of complexity

is constant need to check if data satisfy the

requirements for correct processing.

  • Who should do the checking?
    • Nobody - Not done at all (not safe)
    • Everybody (almost) - Done in several places (for safety reasons)

14

Who should do the checking?

  • Recommended approach is to

systematically use preconditions

  • Allow procedure author to assume that he corresponding precondition is satisfied.
  • Aim is to provide simple style of

programming.

  • Favors readability, maintainability etc.

15

Who should do the checking?

  • Strict preconditions keep the code simple.
    • It is client’s responsibility to ensure call satisfies preconditions.
  • Loose preconditions make the supplier more

fault tolerance.

  • But, supplier must deal explicitly with errors.

An Example: Stack

19

Beware the mal-formed Assertion

  • Assertions are a great way to do “passive”

testing.

  • But, they only check what you express.
  • Can be too “ strong ”.
    • Yields false positives.
  • Can be too “ weak ”.
    • Allows incorrect states to pass unnoticed.

20

Expressing Preconditions

  • Express only logical properties.
    • E.g. empty
    • E.g. full
  • Do not express constraints using underlying representation. - Client will not have access to it.
  • Use methods that check logical properties.
    • In general, there should not be a one-one correspondence between state variables and these methods.
  • Only use methods that have no side-effects.

21

Expressing Postconditions

  • Postconditions should also express logical properties.
  • We classify postcondition expressions into two types: - Physical – These correspond to properties of the state itself. - Logical – These are what the client is interested in (what the method does).
  • Only use methods that have no side-effects.

22

Problems Expressing Postconditions

  • Expressing postconditions is problematic.
    • We can’t always express the result that we desire.
    • Often turns out to be equivalent to the computation of the method.
  • Must result to checking “implied conditions”.
    • Conditions that must necessarily be true if the postcondition is true.
    • But, the converse is not true!

25

Postcondition Problem

26

Postcondition Problem

27

Other Uses of Assertions

  • We also use assertions to express facts about state.
    • Things we expect/assume to be true.
  • Done at various “points of interest”
    • Immediately following method calls.
    • Immediately following complex expressions.
    • At any points where we can detect faults if they have occurred.
    • At points of uncertainty.

Design by Contract Guidance

31

General Principles

  1. Separate queries from commands.
  2. Separate basic queries from derived queries.
  3. For each derived query, write a postcondition that specifies what result will be returned, in terms of one or more basic queries.
  4. For every query and command, decide on a suitable precondition.

32

1. Separate queries from commands.

  • Queries return information, but have no

side effects.

  • Do no change state of objects.
  • Needed for writing contract assertions.
  • Commands have side effects, but return no

information.

  • Can change state of objects.
  • Needed to provide behavior.

33

2. Separate basic

queries from derived queries.

  • Basic queries form a conceptual model.
    • They enable us to state all the essential logical properties about an object.
    • A weaker model will prevent you from stating essential logical properties that you need.
  • Derived queries are based on basic queries.
    • Values of derived queries are defined in terms of the values of the basic queries.

34

3. For each derived query…

  • Write a postcondition that specifies what

result will be returned, in terms of one or

more basic queries

  • This allows us to know the values of the derived queries if we know the values of the basic queries.

37

public class SimpleStack {

/** Put o onto the top of the stack. / public void put( Object o) { ... } /* Remove the item at the top of the stack. / public void remove() { ... } /* Get the item on the top of the stack. */ public Object item() { ... }

}

How to apply Design by Contract

39

General DBC Guidance

  • Define preconditions and postconditions for each public method. - Really. Do it. - You’re going to disable them anyway.
  • Add contract assertions ( require and ensure ) for each public method. - Don’t worry about getters.
  • Add contract assertions for each non-public method.

40

Guidance on Checking Preconditions

  • Explicitly check the precondition prior to

making a call when:

  • You cannot say with certainty that the precondition will always be satisfied (assuming correctness).
  • Otherwise, use assertions to check your

assumptions at the call site.