1 / 9

OOSC –Contracts

OOSC –Contracts. Design By Contract. A powerful technique for writing reliable software. Specifying the software purpose with the implementation. Key elements: Invariant Preconditions Postconditions. Design By Contract.

Download Presentation

OOSC –Contracts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OOSC –Contracts

  2. Design By Contract • A powerful technique for writing reliable software. • Specifying the software purpose with the implementation. • Key elements: • Invariant • Preconditions • Postconditions

  3. Design By Contract • Precondition – The constraints under which the routine will function properly. • Postconditions – The state of the class after the routine execution • The Contract: If you call routine R() with the preconditions satisfied, R() will return satisfying the postconditions. • Invariant – Always holds

  4. When are condition checked?

  5. Invariant • Invariant - @inv • May access all class members or its direct/indirect bases, including private members • Preferable in the class comment

  6. Precondition - @pre May reference class members and arguments Multiple @pre markers are conjugated (AND) Postconditions - @post Use $prev(expression) to access the value at the method entry. Use $ret to denote method’s return value Multiple @post markers are conjugated (AND) Preconditions and Postconditions

  7. Example Precondition /** * @pre !isEmpty() * @post (top == $prev(top- 1)) * @post $ret == elems[top] * @post !isFull */ public synchronized Object pop() { return elems[--top]; } Postconditions

  8. General • Order has no meaning • Recursion – as expected, on every call • Inner classes can access outer classes’ members • Anonymous classes – specify invariant in one of its methods

  9. Union-Find http://www.cs.unm.edu/~rlpm/499/uf.html

More Related