Chair of Software Engineering OOSC - Summer Semester 2004 1 Object-Oriented Software Construction Bertrand Meyer.

Slides:



Advertisements
Similar presentations
Building Bug-Free O-O Software: An Introduction to Design By Contract A presentation about Design By Contract and the Eiffel software development tool.
Advertisements

SCOOP: Simple Concurrent Object-Oriented Programming Extend the pure, strongly typed, object-oriented language Eiffel with a general and powerful concurrency.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 3: Abstract Data Types.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Chair of Software Engineering Software Architecture Prof. Dr. Bertrand Meyer Lecture 6: Exception Handling.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Design by Contract ™. 2 Design by Contract A discipline of analysis, design, implementation, management.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 10, 5 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 25, 30 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Design by Contract. Specifications Correctness formula (Hoare triple) {P} A {Q} – A is some operation (for example, a routine body) – P and Q are predicates.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 15: Exception handling.
-5- Exception handling What is an exception? “An abnormal event” Not a very precise definition Informally: something that you don’t want to happen.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 17: Topological Sort Algorithm.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Software Testing and Quality Assurance
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 11: Design by Contract™
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 10: Project Presentation Ilinca.
Chair of Software Engineering ATOT - Lecture 12, 12 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 26, 30 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 12: Design by Contract™
Chair of Software Engineering ATOT - Lecture 8, 28 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 6 7 October 2008.
Chair of Software Engineering OOSC - Lecture 4 1 Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 9, 30 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Karlstad University Computer Science Design Contracts and Error Management Design Contracts and Errors A Software Development Strategy (anpassad för PUMA)
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 14: Presentation of EiffelStudio.
Chair of Software Engineering 1 Introduction to Programming Exercise Session Week 9 M. Piccioni 17/18 November 2008.
Chair of Software Engineering ATOT - Lecture 11, 7 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 6: Genericity.
Chair of Software Engineering ATOT - Lecture 3, 7 April Advanced Topics in Object Technology Bertrand Meyer.
Software Verification Bertrand Meyer Chair of Software Engineering Lecture 2: Axiomatic semantics.
Eiffel Language and Design by Contract Contract –An agreement between the client and the supplier Characteristics –Expects some benefits and is prepared.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Software Engineering Design by Contract 1 Design by Contract ™
1 © Wolfgang Pelz Design by Contract Design by Contract™ Based on material drawn from: Bertrand.
Fundamentals of Python: From First Programs Through Data Structures
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Contracts for Concurrency - Contracts & Inheritance Aryabrata Basu University of Georgia.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
L13: Design by Contract Definition Reliability Correctness Pre- and post-condition Asserts and Exceptions Weak & Strong Conditions Class invariants Conditions.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 1 COSC3311 – Software Design Loop invariant/variant.
Defensive Programming CNS 3370 Copyright 2003, Fresh Sources, Inc.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
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.
1 Exceptions When the Contract is Broken. 2 Definitions A routine call succeeds if it terminates its execution in a state satisfying its contract A routine.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
Design by Contract. The Goal Ensure the correctness of our software (correctness) Recover when it is not correct anyway (robustness) Correctness: Assertions.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
契约式设计 Design by Contract Institute of Computer Software Nanjing University.
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Programming Languages 2nd edition Tucker and Noonan
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Programming Languages 2nd edition Tucker and Noonan
Design Contracts and Errors A Software Development Strategy
Presentation transcript:

Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer

Chair of Software Engineering OOSC - Summer Semester Lecture 11: Design by Contract™

Chair of Software Engineering OOSC - Summer Semester Contracts and inheritance  Issues: what happens, under inheritance, to  Class invariants?  Routine preconditions and postconditions?

Chair of Software Engineering OOSC - Summer Semester Invariants  Invariant Inheritance rule:  The invariant of a class automatically includes the invariant clauses from all its parents, “and”-ed.  Accumulated result visible in flat and interface forms.

Chair of Software Engineering OOSC - Summer Semester Contracts and inheritance Correct call: if a1. then a1.r (...) -- Here a1. holds. end r+ is require  ensure  r is require  ensure  CA B a1: A a1.r (…) … D

Chair of Software Engineering OOSC - Summer Semester Assertion redeclaration rule  When redeclaring a routine:  Precondition may only be kept or weakened.  Postcondition may only be kept or strengthened.  Redeclaration covers both redefinition and effecting.  Should this remain a purely methodological rule? A compiler can hardly infer e.g. that: n > 1 implies (is stronger) than n ^ * n ^25 > 3

Chair of Software Engineering OOSC - Summer Semester  A simple language rule does the trick!  Redefined version may not have require or ensure.  May have nothing (assertions kept by default), or require else new_pre ensure then new_post  Resulting assertions are:  original_precondition or new_pre  original_postcondition and new_post Assertion redeclaration rule in Eiffel

Chair of Software Engineering OOSC - Summer Semester Don’t call us, we’ll call you deferred class LIST [G] inherit CHAIN [G] feature has (x: G): BOOLEAN is -- Does x appear in list? do from start until after or else found (x) loop forth end Result := not after end

Chair of Software Engineering OOSC - Summer Semester Sequential structures item before after count forthback index 1 start

Chair of Software Engineering OOSC - Summer Semester Sequential structures (cont’d) forth is -- Move cursor to next position. require not after deferred ensure index = old index + 1 end start is -- Move cursor to the first position. deferred ensure empty or else index = 1 end

Chair of Software Engineering OOSC - Summer Semester Sequential structures (cont’d) index: INTEGER is deferred end... empty, found, after,... invariant 0 <= index index <= size + 1 empty implies (after or before) end

Chair of Software Engineering OOSC - Summer Semester Descendant implementations CHAIN * LIST * LINKED_ LIST + ARRAYED_ LIST + BLOCK_ LIST + has+ forth* item* start* forth+ item+ start+ after+ forth+ item+ start+ after+ forth+ item+ start+ after+ after* has*

Chair of Software Engineering OOSC - Summer Semester Implementation variants Arrayed list Linked list File startforthafteritem (x) c := first_cell rewind i := 1 c := c.right i := i + 1 readend_of_file c := Void f c.item i > countt [i]

Chair of Software Engineering OOSC - Summer Semester Methodological notes  Contracts are not input checking tests... ... but they can be used to help weed out undesirable input.  Filter modules: External objects Input and validation modules Processing modules Preconditions here only

Chair of Software Engineering OOSC - Summer Semester Precondition design  The client must guarantee the precondition before the call.  This does not necessarily mean testing for the precondition.  Scheme 1 (testing): if not my_stack.is_full then my_stack.put (some_element) end  Scheme 2 (guaranteeing without testing): my_stack.remove... my_stack.put (some_element)

Chair of Software Engineering OOSC - Summer Semester Another example sqrt (x, epsilon: REAL): REAL is -- Square root of x, precision epsilon require x >= 0 epsilon >= 0 do... ensure abs (Result ^ 2 – x) <= 2 * epsilon * Result end

Chair of Software Engineering OOSC - Summer Semester The contract Client Supplier (Satisfy precondition:) Provide non-negative value and precision that is not too small. (Satisfy postcondition:) Produce square root within requested precision. OBLIGATIONS (From postcondition:) Get square root within requested precision. (From precondition:) Simpler processing thanks to assumptions on value and precision. BENEFITS sqrt

Chair of Software Engineering OOSC - Summer Semester Not defensive programming!  It is never acceptable to have a routine of the form sqrt (x, epsilon: REAL): REAL is -- Square root of x, precision epsilon require x >= 0 epsilon >= 0 do if x < 0 then … Do something about it (?) … else … normal square root computation … end ensure abs (Result ^ 2 – x) <= 2 * epsilon * Result end

Chair of Software Engineering OOSC - Summer Semester Not defensive programming  For every consistency condition that is required to perform a certain operation:  Assign responsibility for the condition to one of the contract’s two parties (supplier, client).  Stick to this decision: do not duplicate responsibility.  Simplifies software and improves global reliability.

Chair of Software Engineering OOSC - Summer Semester Interpreters class BYTECODE_PROGRAM feature verified: BOOLEAN trustful_execute (program: BYTECODE) is require ok: verified do... end distrustful_execute (program: BYTECODE) is do verify if verified then trustful_execute (program) end end verify is do... end end

Chair of Software Engineering OOSC - Summer Semester How strong should a precondition be?  Two opposite styles:  Tolerant: weak preconditions (including the weakest, True: no precondition).  Demanding: strong preconditions, requiring the client to make sure all logically necessary conditions are satisfied before each call.  Partly a matter of taste.  But: demanding style leads to a better distribution of roles, provided the precondition is:  Justifiable in terms of the specification only.  Documented (through the short form).  Reasonable!

Chair of Software Engineering OOSC - Summer Semester A demanding style sqrt (x, epsilon: REAL): REAL is -- Square root of x, precision epsilon -- Same version as before require x >= 0 epsilon >= 0 do... ensure abs (Result ^ 2 – x) <= 2 * epsilon * Result end

Chair of Software Engineering OOSC - Summer Semester sqrt (x, epsilon: REAL): REAL is -- Square root of x, precision epsilon require True do if x < 0 then … Do something about it (?) … else … normal square root computation … computed := True end ensure computed implies abs (Result ^ 2 – x) <= 2 * epsilon * Result end A tolerant style NO INPUT TOO BIG OR TOO SMALL!

Chair of Software Engineering OOSC - Summer Semester Contrasting styles put (x: G) is -- Push x on top of stack. require not is_full do.... end tolerant_put (x: G) is -- Push x if possible, otherwise set impossible to -- True. do if not is_full then put (x) else impossible := True end end

Chair of Software Engineering OOSC - Summer Semester Invariants and business rules  Invariants are absolute consistency conditions.  They can serve to represent business rules if knowledge is to be built into the software.  Form 1 invariant not_under_minimum: balance >= Minimum_balance  Form 2 invariant not_under_minimum_if_normal: normal_state implies (balance >= Minimum_balance)

Chair of Software Engineering OOSC - Summer Semester Loop trouble  Loops are needed, powerful  But very hard to get right:  “off-by-one”  Infinite loops  Improper handling of borderline cases  For example: binary search feature

Chair of Software Engineering OOSC - Summer Semester The answer: assertions  Use of loop variants and invariants.  A loop is a way to compute a certain result by successive approximations.  (e.g. computing the maximum value of an array of integers)

Chair of Software Engineering OOSC - Summer Semester Computing the max of an array  Approach by successive slices: max_of_array (t: ARRAY [INTEGER]): INTEGER is -- Maximum value of array t local i: INTEGER do from i := t.lower Result := lower until i = t.upper loop i := i + 1 Result := Result.max i) end

Chair of Software Engineering OOSC - Summer Semester Loop variants and invariants  Syntax: from init invariant inv-- Correctness property variant var-- Ensure loop termination. until exit loop body end

Chair of Software Engineering OOSC - Summer Semester Maximum of an array (cont’d) max_of_array (t: ARRAY [INTEGER]): INTEGER is -- Maximum value of array t local i: INTEGER do from i := t.lower Result := lower invariant -- Result is the max of the elements of t at indices -- t.lower to i variant t.lower - i until i = t.upper loop i := i + 1 Result := Result.max i) end

Chair of Software Engineering OOSC - Summer Semester A powerful assertion language  Assertion language:  Not first-order predicate calculus  But powerful through:  Function calls  Even allows to express:  Loop properties

Chair of Software Engineering OOSC - Summer Semester Another one…  Check instruction: ensure that a property is True at a certain point of the routine execution.  E.g. Tolerant style example: Adding a check clause for readability.

Chair of Software Engineering OOSC - Summer Semester Precondition design  Scheme 2 (guaranteeing without testing): my_stack.remove check my_stack_not_full: not my_stack.is_full end my_stack.put (some_element)

Chair of Software Engineering OOSC - Summer Semester End of lecture 11