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.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Advertisements

Formal techniques for getting software right: some old ideas and some new tools Applied Formal Methods Research Group David Lightfoot:
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Design by Contract.
Copyright W. Howden1 Programming by Contract CSE 111 6/4/2014.
Ceg860(Prasad)L10DC1 Design by Contract Invariants Pre-post conditions : Rights and Obligations Exceptions : Contract Violations.
Carlos D. Rivera February 28, 2007 Design-by-Contract.
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Design by Contract. Design by contract is the process of developing software based on the notion of contracts between objects, which are expressed as.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
ISBN Chapter 3 Describing Syntax and 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.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
Design by Contract ™. 2 Design by Contract A discipline of analysis, design, implementation, management.
Feb 2003 R McFadyen1 Contracts (Ch 13) Used to help understand requirements more completely based on assertions; assertions are applicable to any.
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.
Jan 2005 Ron McFadyen1 Contracts Used to help understand requirements more completely (and so may not always be necessary) based on assertions;
Software Testing and Quality Assurance: The Testing Perspective Reading Assignment: –John McGregor and David A. Sykes, A Practical Guide to Testing Object-Oriented.
Software Testing and Quality Assurance
1 Advanced Material The following slides contain advanced material and are optional.
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 1 Introduction to Programming Bertrand Meyer Exercise Session 6 7 October 2008.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with.
September 2002 R McFadyen1 Domain Model Use Case Model text diagram SSD System operation contracts Design Model Figure 13.3.
1 Introduction to: Design by Contract Fall 2005 OOPD John Anthony.
Design by Contract David Talby. Software Correctness When is a class correct? – It ’ s a relative concept; what is required? – But it ’ s the correct.
Describing Syntax and Semantics
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.
1 © Wolfgang Pelz Design by Contract Design by Contract™ Based on material drawn from: Bertrand.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
Computer Science 340 Software Design & Testing Design By Contract.
Ranga Rodrigo. Class is central to object oriented programming.
PRAGMATIC PARANOIA Steven Hadfield & Anthony Rice.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Contract based programming Using pre- and post-conditions, and object invariants Contract based programming1.
Design by Contract in Java Concept and Comparison.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
More About Classes Ranga Rodrigo. Information hiding. Copying objects.
OOSCCh11r1:DbC RJL Slide #1 OOSC: Ch11 vs. Predecessors [Meyer:] Equipped with the basic concepts of class, object and genericity, you can by now.
Contracts for Concurrency - Contracts & Inheritance Aryabrata Basu University of Georgia.
© Paul Ammann, 2008 Design by Contract Paul Ammann CS/SWE 332.
Design by Contract Ranga Rodrigo based on Mark Priestley's Lectures.
Chapter 3 Part II Describing Syntax and Semantics.
1 Devon M. Simmonds, Computer Science Department Design by Contract Devon M. Simmonds Computer Science Department University of North Carolina, Wilmington.
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.
Ceg860 (Prasad)L17IT1 Inheritance Techniques Subcontracting Anchored Types.
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.
Defensive Programming CNS 3370 Copyright 2003, Fresh Sources, Inc.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
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.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
י"ח/תשרי/תשע"ח Design by Contract David Talby.
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Used to help understand requirements more completely
Design by Contract in Python: Present and Future Aaron Bingham
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.
Computer Science 340 Software Design & Testing
Class Invariants Pre-conditions and post-conditions describe the properties of individual methods. A class invariant is a global property of the instances.
Presentation transcript:

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 is respected. l The routine ensures that after completion its post-condition is respected.

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

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

Methods II l The same in iContract syntax: l //** return Square root of x x >= 0 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

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.

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.

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”.

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.

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.

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

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.

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.

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.

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

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

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!

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 

{ 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

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.

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!