Chair of Software Engineering ATOT - Lecture 11, 7 May 2003 1 Advanced Topics in Object Technology Bertrand Meyer.

Slides:



Advertisements
Similar presentations
Ceg860(Prasad)L10DC1 Design by Contract Invariants Pre-post conditions : Rights and Obligations Exceptions : Contract Violations.
Advertisements

Building Bug-Free O-O Software: An Introduction to Design By Contract A presentation about Design By Contract and the Eiffel software development tool.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Exercise Session 1: Eiffel Introduction.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 3: Abstract Data Types.
Chair of Software Engineering Software Architecture Bertrand Meyer, Carlo A. Furia, Martin Nordio ETH Zurich, February-May 2011 Lecture 15: Design by.
Chair of Software Engineering Software Architecture Prof. Dr. Bertrand Meyer Lecture 4: Design by Contract.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Software Architecture Prof. Dr. Bertrand Meyer Lecture 6: Exception Handling.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Design by Contract ™. 2 Design by Contract A discipline of analysis, design, implementation, management.
Eiffel Programming Language. Chad Frommeyer CSC 407/507 Fall 2005 Dr. Richard Fox.
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.
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 ATOT - Lecture 24, 25 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
1 Specifying Object Interfaces. 2 Major tasks in this stage: --are there any missing attributes or operations? --how can we reduce coupling, make interface.
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.
Static and Dynamic Contract Verifiers For Java Hongming Liu.
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 OOSC - Lecture 4 1 Object-Oriented Software Construction 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 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 9: Abstraction.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 5: Project and EiffelStudio Presentation.
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 ATOT - Lecture 20, 11 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 2, 2 April 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.
Chair of Software Engineering ATOT - Lecture 14, 19 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
1 Introduction to: Design by Contract Fall 2005 OOPD John Anthony.
Software Verification Bertrand Meyer Chair of Software Engineering Lecture 2: Axiomatic semantics.
Chair of Software Engineering Automatic Verification of Computer Programs.
Eiffel Language and Design by Contract Contract –An agreement between the client and the supplier Characteristics –Expects some benefits and is prepared.
Adding Contracts to Ada Ehud Lamm Adding Design By Contract to Ada.
Chair of Software Engineering ATOT - Lecture 7, 23 April Advanced Topics in Object Technology Bertrand Meyer.
Software Engineering Design by Contract 1 Design by Contract ™
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 9: Abstraction.
1 © Wolfgang Pelz Design by Contract Design by Contract™ Based on material drawn from: Bertrand.
Computer Science 340 Software Design & Testing Design By Contract.
Ranga Rodrigo. Class is central to object oriented programming.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Tammy Dahlgren with Tom Epperly, Scott Kohn, and Gary Kumfert Center for Applied Scientific Computing Common Component Architecture Working Group October.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 5: Invariants and Logic.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
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.
1 Assertions. 2 A boolean expression or predicate that evaluates to true or false in every state In a program they express constraints on the state that.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
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.
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
Specifying Object Interfaces
The following slides contain advanced material and are optional.
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Computer Science 340 Software Design & Testing
Contract-Based Programming with/without Ada 2012
Design Contracts and Errors A Software Development Strategy
Presentation transcript:

Chair of Software Engineering ATOT - Lecture 11, 7 May Advanced Topics in Object Technology Bertrand Meyer

Chair of Software Engineering ATOT - Lecture 11, 7 May Lecture 11: Design by Contract™

Chair of Software Engineering ATOT - Lecture 11, 7 May Contracts: run-time effect  Compilation options (per class, in Eiffel):  No assertion checking  Preconditions only  Preconditions and postconditions  Preconditions, postconditions, class invariants  All assertions

Chair of Software Engineering ATOT - Lecture 11, 7 May A contract violation is not a special case  For special cases (e.g. “if the sum is negative, report an error...”) use standard control structures (e.g. if... then... else...).  A run-time assertion violation is something else: the manifestation of A DEFECT (“BUG”)

Chair of Software Engineering ATOT - Lecture 11, 7 May The contract language  Language of boolean expressions (plus old):  No predicate calculus (i.e. no quantifiers,  or ).  Function calls permitted (e.g. in a STACK class): put (x: G) is -- Push x on top of stack. require not is_full do … ensure not is_empty end remove (x: G) is -- Pop top of stack. require not is_empty do … ensure not is_full end

Chair of Software Engineering ATOT - Lecture 11, 7 May The contract language (cont’d)  First order predicate calculus may be desirable, but not sufficient anyway.  Example: “The graph has no cycles”.  In assertions, use only side-effect-free functions.  Use of iterators provides the equivalent of first-order predicate calculus in connection with a library such as EiffelBase or STL. For example (Eiffel agents, i.e. routine objects): my_integer_list.for_all (agent is_positive (?)) with is_positive (x: INTEGER): BOOLEAN is do Result := (x > 0) end

Chair of Software Engineering ATOT - Lecture 11, 7 May The imperative and the applicative do balance := balance - sum ensure balance = old balance - sum PRESCRIPTIVEDESCRIPTIVE How? Operational Implementation Command Instruction Imperative What? Denotational Specification Query Expression Applicative

Chair of Software Engineering ATOT - Lecture 11, 7 May What are contracts good for?  Writing correct software (analysis, design, implementation, maintenance, reengineering).  Documentation (the “contract” form of a class).  Effective reuse.  Controlling inheritance.  Preserving the work of the best developers.  Quality assurance, testing, debugging (especially in connection with the use of libraries).  Exception handling.

Chair of Software Engineering ATOT - Lecture 11, 7 May A contract violation is not a special case  For special cases (e.g. “if the sum is negative, report an error...”) use standard control structures (e.g. if... then... else...).  A run-time assertion violation is something else: the manifestation of A DEFECT (“BUG”)

Chair of Software Engineering ATOT - Lecture 11, 7 May Contracts and quality assurance  Precondition violation: Bug in the client.  Postcondition violation: Bug in the supplier.  Invariant violation: Bug in the supplier. {P} A {Q}

Chair of Software Engineering ATOT - Lecture 11, 7 May Contracts and bug types  Preconditions are particularly useful to find bugs in client code: YOUR APPLICATION COMPONENT LIBRARY your_list.insert (y, a + b + 1) i <= count + 1 insert (x: G; i: INTEGER) is require i >= 0 class LIST [G] …

Chair of Software Engineering ATOT - Lecture 11, 7 May Contracts and quality assurance  Use run-time assertion monitoring for quality assurance, testing, debugging.  Compilation options (reminder):  No assertion checking  Preconditions only  Preconditions and postconditions  Preconditions, postconditions, class invariants  All assertions

Chair of Software Engineering ATOT - Lecture 11, 7 May Contracts missed  Ariane 5 (see Jézéquel & Meyer, IEEE Computer, January 1997)  Lunar Orbiter Vehicle  Failure of air US traffic control system, November 2000  Y2K  etc. etc. etc.

Chair of Software Engineering ATOT - Lecture 11, 7 May Contracts and quality assurance  Contracts enable QA activities to be based on a precise description of what they expect.  Profoundly transform the activities of testing, debugging and maintenance. “I believe that the use of Eiffel-like module contracts is the most important non-practice in software world today. By that I mean there is no other candidate practice presently being urged upon us that has greater capacity to improve the quality of software produced.... This sort of contract mechanism is the sine-qua-non of sensible software reuse. ” Tom de Marco, IEEE Computer, 1997

Chair of Software Engineering ATOT - Lecture 11, 7 May Debugging with contracts: an example  This example will use a live demo from EiffelStudio, with a “planted” error leading to a precondition violation.  The example uses both the browsing and debugging mechanisms.

Chair of Software Engineering ATOT - Lecture 11, 7 May To understand the example: list conventions item before after count forthback index 1 start

Chair of Software Engineering ATOT - Lecture 11, 7 May Linked list representation (LINKABLE) (LINKED_LIST) first_element count

Chair of Software Engineering ATOT - Lecture 11, 7 May Adding and catching a bug  In class STARTER, procedure make_a_list, replace the first call to extend by a call to put.  Execute system. What happens?  Use browsing mechanisms to find out what’s wrong (violated precondition).  To understand, consider what the diagram of page 16 becomes when the number of list items goes to zero. 16

Chair of Software Engineering ATOT - Lecture 11, 7 May Contract monitoring  Enabled or disabled by compile-time options.  Default: preconditions only.  In development: use “all assertions” whenever possible.  During operation: normally, should disable monitoring. But have an assertion-monitoring version ready for shipping.  Result of an assertion violation: exception.  Ideally: static checking (proofs) rather than dynamic monitoring.

Chair of Software Engineering ATOT - Lecture 11, 7 May Contracts and documentation Recall example class: class ACCOUNT create make feature {NONE} -- Implementation add (sum: INTEGER) is -- Add sum to the balance (secret procedure). do balance := balance + sum ensure increased: balance = old balance + sum end deposits: DEPOSIT_LIST withdrawals: WITHDRAWAL_LIST

Chair of Software Engineering ATOT - Lecture 11, 7 May Class example (cont’d) feature {NONE} -- Initialization make (initial_amount: INTEGER) is -- Set up account with initial_amount. require large_enough: initial_amount >= Minimum_balance do deposit (initial_amount) create deposits.make create withdrawals.make ensure balance_set: balance = initial_amount end feature -- Access balance: INTEGER -- Balance Minimum_balance: INTEGER is Minimum balance

Chair of Software Engineering ATOT - Lecture 11, 7 May Class example (cont’d) feature -- Deposit and withdrawal operations deposit (sum: INTEGER) is -- Deposit sum into the account. require not_too_small: sum >= 0 do add (sum) deposits.extend (create {DEPOSIT}.make (sum)) ensure increased: balance = old balance + sum end

Chair of Software Engineering ATOT - Lecture 11, 7 May Class example (cont’d) withdraw (sum: INTEGER) is -- Withdraw sum from the account. require not_too_small: sum >= 0 not_too_big: sum <= balance – Minimum_balance do add (– sum) withdrawals.extend (create {WITHDRAWAL}.make (sum)) ensure decreased: balance = old balance – sum one_more: withdrawals.count = old withdrawals.count + 1 end

Chair of Software Engineering ATOT - Lecture 11, 7 May Class example (end) may_withdraw (sum: INTEGER): BOOLEAN is -- Is it permitted to withdraw sum from the -- account? do Result := (balance - sum >= Minimum_balance) end invariant not_under_minimum: balance >= Minimum_balance consistent: balance = deposits.total – withdrawals.total end

Chair of Software Engineering ATOT - Lecture 11, 7 May Contract form: Definition  Simplified form of class text, retaining interface elements only:  Remove any non-exported (private) feature.  For the exported (public) features:  Remove body (do clause).  Keep header comment if present.  Keep contracts: preconditions, postconditions, class invariant.  Remove any contract clause that refers to a secret feature. (This raises a problem; can you see it?)

Chair of Software Engineering ATOT - Lecture 11, 7 May Export rule for preconditions  In  some_property must be exported (at least) to A, B and C!  No such requirement for postconditions and invariants. feature {A, B, C} r (…) is require some_property

Chair of Software Engineering ATOT - Lecture 11, 7 May Contract form of ACCOUNT class class interface ACCOUNT create make feature balance: INTEGER -- Balance Minimum_balance: INTEGER is Minimum balance deposit (sum: INTEGER) -- Deposit sum into the account. require not_too_small: sum >= 0 ensure increased: balance = old balance + sum

Chair of Software Engineering ATOT - Lecture 11, 7 May Contract form (cont’d) withdraw (sum: INTEGER) -- Withdraw sum from the account. require not_too_small: sum >= 0 not_too_big: sum <= balance – Minimum_balance ensure decreased: balance = old balance – sum one_more: withdrawals.count = old withdrawals.count + 1 may_withdraw (sum: INTEGER): BOOLEAN -- Is it permitted to withdraw sum from the -- account? invariant not_under_minimum: balance >= Minimum_balance consistent: balance = deposits.total – withdrawals.total end

Chair of Software Engineering ATOT - Lecture 11, 7 May Flat, interface  Flat form of a class: reconstructed class with all the features at the same level (immediate and inherited). Takes renaming, redefinition etc. into account.  The flat form is an inheritance-free client- equivalent form of the class.  Interface form: the contract form of the flat form. Full interface documentation.

Chair of Software Engineering ATOT - Lecture 11, 7 May Uses of the contract and interface forms  Documentation, manuals  Design  Communication between developers  Communication between developers and managers

Chair of Software Engineering ATOT - Lecture 11, 7 May Contracts and reuse  The contract form — i.e. the set of contracts governing a class — should be the standard form of library documentation.  Reuse without a contract is sheer folly.  See the Ariane 5 example.  See Jézéquel & Meyer, IEEE Computer, January 1997.

Chair of Software Engineering ATOT - Lecture 11, 7 May End of lecture 11