Programming by Contract

Cards (19)

  • An important aspect of object oriented design is reuse
  • Main goal of object oriented design and programming is to improve the quality of software
  • Design by contract presents a set of principles to produce dependable and robust object oriented software
  • Client
    requests a service
  • Supplier

    supplies the service
  • Contract
    the agreement between the client and the supplier
  • Each party expects some benefits from the contract and is prepared to incur some obligations to obtain them. These benefits and obligations are documented in a contract document
  • Benefit of the client is the obligation of the supplier, and vice versa.
  • A contract document between a client and a supplier protects both sides
  • If a party fulfills its obligations it is entitled to its benefits
  • No Hidden Clauses Rule
    no requirement other than the obligations written in the contract can be imposed on a party to obtain the benefits
  • Design by contract promotes using pre and post-conditions (written as assertions) as a part of module design
  • The pre and postconditions are assertions, i.e., they are expressions which evaluate to true or false
  • If there is no precondition or postcondition, then the precondition or postcondition is assumed to be true
  • Defensive programming
    an approach that promotes putting checks in every module to detect unexpected situations
  • In Design by Contract the responsibility assignment is clear and it is part of the module interface
  • Frequently a programmer must communicate precisely what a function accomplishes, without any indication of how the function does its work.
  • precondition
    statement indicates what must be true before the function is called.
  • postcondition
    statement indicates what will be true when the function finishes its work.