Software engineeringLesson 4 of 4 in the course Requirements and design

Design by Contract: Obligations, Inheritance, and Verification

5 flashcards · 5 quiz questions · 5 match pairs · 5 fill-in sentences · English

Lesson progress
0 / 5

Your progress is kept in this browser only.Sign up to save it

Summary

Design by contract (DbC) is an approach to software design that makes the expectations and obligations between software components explicit. This lesson introduces the contract metaphor and the three central elements used to specify a component's behavior.

In business, a contract defines what each party must do and what each party may expect in return. DbC applies the same idea to software components. A client calls on a supplier—for example, a method or service—and the contract specifies their mutual obligations and benefits.

All flashcards

What is design by contract?
A software design approach that specifies component behavior through precise, verifiable expectations and obligations, including preconditions, postconditions, and invariants.
What is a precondition, and who is responsible for it?
A precondition states what must be true before an operation is called. The client is responsible for satisfying it.
What is a postcondition?
A postcondition states what the supplier guarantees after an operation completes, provided the client met the precondition.
What is a class invariant?
A property defining a valid state of a class. It is expected to hold when an operation begins and must hold again when the operation completes.
What are the three questions used to design a contract?
What does the contract expect? What does it guarantee? What does it maintain? These correspond to preconditions, postconditions, and invariants.

Quiz questions

  1. In a Hoare-style view of a method call, what should hold after a valid call?Answer: The class invariant and the method's postcondition.
  2. Which contract changes are permitted when a subclass redefines a method?Answer: Weaken preconditions and strengthen postconditions and invariants.
  3. How does design by contract relate to software testing?Answer: Contract checks complement unit, integration, and system testing.
  4. In defensive programming, what may a supplier do when a client violates a precondition?Answer: Check the request and return an error or raise an exception.
  5. Who coined the term Design by Contract in connection with Eiffel?Answer: Bertrand Meyer.

Based on the Wikipedia article Design by contract, licensed CC BY-SA 4.0. Adapted into a summary, flashcards and quizzes.

All Software engineering lessons