1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Formal techniques for getting software right: some old ideas and some new tools Applied Formal Methods Research Group David Lightfoot:
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.
Building Bug-Free O-O Software: An Introduction to Design By Contract A presentation about Design By Contract and the Eiffel software development tool.
Lecture 8: Testing, Verification and Validation
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
Reasoning About Code; Hoare Logic, continued
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
SOFTWARE TESTING. INTRODUCTION  Software Testing is the process of executing a program or system with the intent of finding errors.  It involves any.
Design by Contract. Design by contract is the process of developing software based on the notion of contracts between objects, which are expressed as.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 19: Minding Ps & Qs: Axiomatic.
Error Management with Design Contracts Karlstad University Computer Science Error Management with Design Contracts Eivind J. Nordby, Martin Blom, Anna.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Program Proving Notes Ellen L. Walker.
Weakest pre-conditions and towards machine consistency Saima Zareen.
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.
Software Engineering and Design Principles Chapter 1.
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.
On the Relation between Design Contracts and Errors Karlstad University Computer Science On the Relation Between Design Contracts and Errors A Software.
-5- Exception handling What is an exception? “An abnormal event” Not a very precise definition Informally: something that you don’t want to happen.
ESC Java. Static Analysis Spectrum Power Cost Type checking Data-flow analysis Model checking Program verification AutomatedManual ESC.
Software Testing and Quality Assurance
Chair of Software Engineering ATOT - Lecture 12, 12 May Advanced Topics in Object Technology 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.
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.
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.
Procedure specifications CSE 331. Outline Satisfying a specification; substitutability Stronger and weaker specifications - Comparing by hand - Comparing.
CS Fall 2007 Dr. Barbara Boucher Owens. CS 2 Text –Main, Michael. Data Structures & Other Objects in Java Third Edition Objectives –Master building.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
OOSCCh11r1:DbC RJL Slide #1 OOSC: Ch11 vs. Predecessors [Meyer:] Equipped with the basic concepts of class, object and genericity, you can by now.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Reasoning about programs March CSE 403, Winter 2011, Brun.
© Paul Ammann, 2008 Design by Contract Paul Ammann CS/SWE 332.
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
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.
Defensive Programming CNS 3370 Copyright 2003, Fresh Sources, Inc.
1 Introduction 1. Why Data Structures? 2. What AreData Structure? 3. Phases of Software Development 4. Precondition and Postcondition 5. Examples.
Program Correctness. The designer of a distributed system has the responsibility of certifying the correctness of the system before users start using.
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.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
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 Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Used to help understand requirements more completely
Topics: jGRASP editor ideosyncrasies assert debugger.
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
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.
Computer Science 340 Software Design & Testing
Programming Languages 2nd edition Tucker and Noonan
Design Contracts and Errors A Software Development Strategy
Presentation transcript:

1 Design by Contract Building Reliable Software

2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification  To be correct a program must correspond with its specification  print("Hello world") Is neither correct nor incorrect Correspondence is one of the cornerstones of building reliable software  Viewing the system – or part of it – from different perspectives without contradiction

3 Correctness Formulae A is some operation  One or more program text statements, including procedure calls Pre & Post are the preconditions and postconditions for the operation { Pre } A { Post } Any execution of A, starting in a state where Pre holds, will terminate in a state where Post holds

4 { P } A { Q } Also called Hoare triples  Mathematical notation  P & Q are assertions Example  { x  9 } x : = x + 5 { x  13 }  Note assertions only need to be true  Can assertions be good? the best?  What do we mean by good with respect to assertions?

5 Weak and Strong Assertions Suppose you are given a set of integers  { } An assertion can be used to describe the integers in the set  1 – a set of some integers  { p: INTEGER p }  2 – a set of even integers  { p: INTEGER | p mod 2 = 0 p }  3 – a set of powers of two  { p: INTEGER 2 ** p }  4 – set of powers of two with positive exponent  { p: INTEGER | p > 0 2 ** p } Stronger Weaker

6 Weak and Strong Assertions – 2 The stronger the assertion the closer the description comes to specifying the actual set In general  Weak assertions describe bigger sets than strong assertions In programming  The weaker the assertion the more cases that must be handled  For precondition – more input states  For postcondition – more output states

7 Job Hunting Suppose you are looking for a job where you have to do A If P is weak you have to handle many cases, if P is strong you have fewer cases to handle What do you look for to make your job easier? What does the employer look for to get the most work out of you? { P } A { Q }

8 Strongest Precondition No input condition is acceptable  You do not have to do any work as the conditions are never right for you to do anything  Independent of postcondition – A is never executed The supplier – you – has no responsibility, do no work – take the job !!! The client – employer – has all the responsibility, has to do all the work as they get no work out of you { False } A {... }

9 Weakest Precondition Any input condition is acceptable  As an employee you have to handle all possible situations to make Q true  This is the most work on your part – if you are lazy you, stay away from this job  The employer loves this, they get the most out of you The supplier – you – does all the work and has all the responsibility – taking the job depends upon Q The client – employer – has no responsibility, does no work { True } A { Q }

10 Precondition Conclusions The stronger the precondition the better for the supplier, the worse for the client There is a tradeoff In practice  Have the weakest precondition that makes the task feasible  Satisfy the most clients  Supplier is able to satisfy the postcondition with reasonable effort

11 Weakest Postcondition All output conditions are acceptable  You have an easy job, as anything you do is acceptable as long as you do something  Independent of precondition – input not linked to output The supplier – you – has minimum responsibility, do minimum work – next best thing to strongest precondition The client – employer – has all the responsibility, has to do all the work as they may not get any useful work out of you {... } A { True }

12 Strongest Postcondition No output condition is acceptable  You have to work forever without achieving your goal, you are doomed to failure The supplier – you – does all the work and has all the responsibility but never achieves anything The client – employer – has no responsibility, does no work but does not get anything done {... } A { False } Strongest postcondition is actually not good for either supplier or client

13 Postcondition Conclusions The stronger the postcondition the better for the client, the worse for the supplier There is a tradeoff In practice  Have the strongest postcondition that makes the task feasible  Satisfy the most clients  Supplier is able to satisfy the postcondition with reasonable effort

14 Benefits & Obligations ObligationsBenefits Client Supplier from preconditions row & col are in range from postconditions get requested element if it exists from postconditions return requested element, if it exists from preconditions knows row and col are in range

15 Get more – check less Less programming – Non Redundancy Principle  Under no circumstances shall the body of a routine ever test for the routine's precondition  Redundancy leads to  software bloat –both size & execution time  complexity  more sources of error Clearly indicate who has what responsibility  supplier  client

16 Defensive Programming Opposite of design by contract  Every routine checks its input regardless of preconditions  Effectively precondition is the weakest – True Every one is responsible ==> No one accepts responsibility  Can always point to someone else Need the notion of  The buck stops here Defensive programming is undefendable

17 Not Input Checking Contracts are about software software communication  NOT the following  software human  software real world Example input routine  require: numeric key to be pressed  Wishful thinking – cannot guarantee person will only press numeric key  Not a contract  Can only expect any key may be pressed

18 Input Subsystem System Convert unreliable real world message to reliable system message M M Within system have design by contract

19 Assertion Violation Rules Rule 1  A run time assertion violation is the manifestation of a bug in the software Rule 2  A precondition violation is the manifestation of a bug in the client  A postcondition violation is the manifestation of a bug in the supplier

20 Definitions Error  A wrong decision made during software development Defect – bug sometimes means this  Property of software that may cause the system to deviate from its intended behaviour Fault – bug sometimes means this  The event in which software deviates from its intended behaviour Error ==> Defect ==> Fault

21 Imperative vs Applicative Not redundant  Body is imperative – a description of how  Computing – changes state  Ensure is applicative – a description of what  Mathematics – does not change state, either true or false full : BOOLEAN is do Result := ( count = capacity ) ensure Result = (count = capacity ) end

22 Imperative vs Applicative – 2 Alternate bodies are possible if count = capacity then Result := True else Result := False end if count = capacity then Result := True end

23 Terminology Implementation Instruction How Imperative Prescription Specification Expression What Applicative Description ComputingMathematics

24 Reasonable Preconditions Preconditions appear in the official documentation given to clients Possible to justify the need for the preconditions in terms of the specification only Every feature appearing in preconditions is available to every client to which the feature is available  No surprises

25 Correctness of a Class A class C is correct with respect to its assertions if and only if  For any valid set of arguments A p to a creation procedure P  { Def C and pre p (A P ) } Body P { post P (A P ) and inv } Where  Def C assert attributes of C have default values  pre P are the preconditions of P  post P are the postconditions of P  inv are the class invariants C1

26 Correctness of a Class – 2  For every exported routine R and any set of valid arguments A R  { pre R (A R ) and inv } Body R { post R (A R ) and inv } C2