Presentation is loading. Please wait.

Presentation is loading. Please wait.

DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.

Similar presentations


Presentation on theme: "DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition."— Presentation transcript:

1 DBC NOTES

2 Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition is respected. l The routine ensures that after completion its post-condition is respected.

3 Design by Contract l Created by Bertrand Meyer, in Eiffel l Each class defines a contract, by placing assertions inside the code l Assertions are just Boolean expressions Eiffel: identified by language keywords iContract: identified by javadoc attributes l Assertions have no effect on execution l Assertions can be checked or ignored

4 Methods l Each feature is equipped with a precondition and a postcondition l double sqrt (double x) l require l x >= 0 l do l … l ensure l result * result == x l end

5 Methods II l The same in iContract syntax: l //** return Square root of x l @pre x >= 0 l @post return * return == x */ l double sqrt (double x) { … } l Assertions are just Boolean expressions Except result and old in postconditions Function calls are allowed, but… Don’t modify data: ++i, inc(x), a = b

6 Design by Contract Consider the relationship between a class and its clients: Without such a precise definition we cannot have a significant degree of trust in large software systems. Contract violations lead to run-time errors, i.e. exceptions. Class B m1() contract Class A DbC views this as a formal agreement, or contract, expressing each party’s rights and obligations. claims responsibilities So we will also consider exception handling.

7 regardless of the size of the program Software Correctness Suppose someone comes to you with a program, and asks “Is this program correct?”. This question is meaningful only if there is a precise description of what the program is supposed to do Such a description is called a specification. A specification must be precise. Otherwise it is not possible to reason about correctness.

8 Expressing a Specification A correctness formula is an expression of the form: { P } A { Q } It means: P and Q are assertions (logical predicates): P is the pre-condition, Q the post-condition. E.g. {x  9} x  x + 5 {x  13} Let A be some operation. e.g. a single instruction or a whole method “Any execution of A, starting in a state where P holds, will terminate in a state where Q holds”.

9 Pre- and Post-conditions { P } A { Q } post-conditionpre-condition – expresses the constraints under which a method will function properly – expresses properties of the state resulting from a method’s execution – applies to all calls of the method both from within the class and from clients. – expresses a guarantee that the method will yield a state satisfying certain properties, assuming it has been called with the pre-condition satisfied. A correct system will never execute a call in a state that does not satisfy the pre-condition of the called method. A correct system will always deliver a state that satisfies the post- condition of the called method.

10 Contracting for Software Reliability Class B m(x); contractClass A pre: p post: q “If you promise to call m with p satisfied, then I, in return, promise to deliver a final state in which q is satisfied.” Consider Class B says to its clients: p binds the client A. It is an obligation for A but a benefit for B. q binds the supplier B. It is an obligation for B but a benefit for A.

11 ObligationsBenefits Client ( A ) Supplier ( B ) E.g. Consider the put operation for stacks: Must satisfy precondition: Only call put(x) on a non- full stack. (From postcondition) Get stack updated. Must satisfy postcondition: Update stack with x on top. (From precondition) Simpler processing, can assume stack not full. B.put(x) x a b c a b c Class B put(x); contractClass A pre:… post:… Stack not full Stack updated

12 Class Invariants Preconditions and postconditions describe the properties of individual methods. A class invariant is a global property of the instances of a class, which must be preserved by all methods. A class invariant is an assertion in the class definition. E.g. a stack class might have the following class invariant: count >= 0 and count <= capacity and stack_is_empty = (count = 0) Class Stack Attrs … Methods … Inv:… An invariant may link attributes (e.g. count and capacity ), or functions (e.g. stack_is_empty ), or attributes and functions.

13 An invariant for a class C must be satisfied by every instance of C at all “stable” times. “Stable” times are those in which the instance is in an observable state S1 S2 S3 Si is an observable state a.new(...) a.m1(...) a.m2(...) is not an observable state i.e. on instance creation and before and after every method call.

14 Before the constructor is executed, the invariant is meaningless — min and max are null. public class RealRange { private RealNumber min, max; public RealRange(RealNumber min, RealNumber max) { this.min = min; this.max = max; } public void setRange(RealNumber newMin, RealNumber newMax) {this.min = newMin; this.max = newMax;} Example E.g. a (mutable) class representing a range of real numbers: The constructor RealRange establishes the invariant. The invariant is re-established by the time the method setRange() terminates. Clearly this class should have an invariant: min <= max. During the execution of setRange(), min may temporarily be more than max.

15 The Invariant Rule An assertion I is a correct invariant for a class C if and only if: Every constructor of C, when applied to arguments satisfying its precondition in a state where the attributes have their default values, yields a state satisfying I. S1 S2 S3 a.new(...) a.m1(...) a.m2(...) constructor with pre-condition satisfies I

16 Every method of the class, when applied to arguments and a state satisfying both I and the method’s precondition, yields a state satisfying I. S1 S2 S3 a.new(...) a.m1(...) a.m2(...) method with pre-condition satisfies I

17 DbC and Inheritance What happens to assertions when classes are inherited? How can assertions be “preserved” in the face of redeclaration (overriding) and dynamic binding? Actually assertions help maintain the semantics of classes and methods when they are inherited. pre-, post-conditions and invariants Only through DbC can we understand what inheritance is really about!

18 Invariants The invariants of all the parents of a class apply to the class itself. The parents’ invariants are added (logically “and”ed) to the class’s own invariants. The parents’ invariants need not be repeated in the class. Class Parent Attrs … Methods … Inv: P Class Child Attrs … Methods … Inv: C Class Parent Attrs … Methods … Inv: P Class Child Attrs … Methods … Inv: P  C 

19 { p } m { q } post-conditionpre-condition Pre and Postconditions A method redeclaration may only: replace the original precondition by one equal or weaker replace the original postcondition by one equal or stronger The new version must accept all calls that were acceptable to the original. The new version must guarantee at least as much as the original. It may, but does not have to, accept more cases. It may, but does not have to, guarantee more. e.g. replace pre: x=10 by pre: x<=10 e.g. replace post: x<=10 by post: x=10

20 Summary Software reliability requires precise specifications which are honoured by both the supplier and the client. DbC uses assertions (pre and postconditions, invariants) as a contract between supplier and client. DbC works equally well under inheritance.

21 DbC & Inheritance Software reliability requires precise specifications which are honoured by both the supplier and the client. Remember the LSP! “The subclass must require less and ensure more” )Meyer, OOSC) The only question is how to ensure this property!


Download ppt "DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition."

Similar presentations


Ads by Google