Design by Contract. The Goal Ensure the correctness of our software (correctness) Recover when it is not correct anyway (robustness) Correctness: Assertions.

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

Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
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.
Carlos D. Rivera February 28, 2007 Design-by-Contract.
Semantics Static semantics Dynamic semantics attribute grammars
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
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.
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;
Copyright W. Howden1 Lecture 13: Programming by Contract.
1 Specifying Object Interfaces. 2 Major tasks in this stage: --are there any missing attributes or operations? --how can we reduce coupling, make interface.
Software Testing and Quality Assurance
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.
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
Karlstad University Computer Science Design Contracts and Error Management Design Contracts and Errors A Software Development Strategy (anpassad för PUMA)
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.
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.
Adding Contracts to Ada Ehud Lamm Adding Design By Contract to Ada.
Sept Ron McFadyen1 Extend Relationship.
Abstract data types What does ‘ abstract ’ mean? From Latin: to ‘ pull out ’— the essentials –To defer or hide the details –Abstraction emphasizes essentials.
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.
1 Debugging and Testing Overview Defensive Programming The goal is to prevent failures Debugging The goal is to find cause of failures and fix it Testing.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
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/
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.
Design by Contract Ranga Rodrigo based on Mark Priestley's Lectures.
What is Testing? Testing is the process of finding errors in the system implementation. –The intent of testing is to find problems with the system.
Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, Mitchell, R., and McKim, Design by Contract,
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.
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.
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.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
י"ח/תשרי/תשע"ח Design by Contract David Talby.
Class Invariants Class invariants are logical conditions to ensure the correct working of a class. Class invariants must hold true when an object is created,
Chapter 6 CS 3370 – C++ Functions.
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.
Design by Contract in Python: Present and Future Aaron Bingham
Design by Contract Fall 2016 Version.
Specifying Object Interfaces
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
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
Unit Testing.
Presentation transcript:

Design by Contract

The Goal Ensure the correctness of our software (correctness) Recover when it is not correct anyway (robustness) Correctness: Assertions Robustness: Exception handling DBC: Relationship between class and client is a formal agreement

What Good Is It? Aid in documentation Aid in debugging Reliability (construct correct programs) Example: Ariane 5 crash, $500 million loss –Conversion from a 64 bit # to 16 bit –The number didn’t fit in 16 bits –Analysis had previously shown it would, so monitoring that assertion was turned off

Software Correctness Someone shows you a 300K line C program. Is it correct? What’s correct? You need a specification. Consider: x = y + 1; Possible specifications: –“Make sure x and y have different values” –“Make sure x has a negative value” (incorrect!)

Expressing a Specification: Assertions in C assert(x<0); Boolean expression Ignored unless in DEBUG mode If true, proceed, if false, abort Can get varying behavior in DEBUG and non-debug modes Eiffel gives you fine grained control on which assertions get checked

Expressing a Specification Correctness formulae (Hoare triples) –{P} A {Q} –{x >= 9} foo() {x >= 13} –{False} A {…} -- the caller erred just by causing this code to be invoked –{…} A {True} -- Must terminate

Preconditions and Postconditions The same idea, on a per-method basis Input requirements: preconditions Output requirements: postconditions preconditions: Caller’s promise to the method postconditions: Method’s promise to the caller

Example class MyStack[G] feature count: INTEGER push(x: G) is require not full do … -- code to perform the push ensure not empty top = x count = old count + 1 end

Contract Benefits and Obligations Satisfy precondition: Only call push(x) if the stack is not full. Satisfy postcondition: Update repr to have x on top, count increased by 1, not empty. From precondition: Simpler implementation thanks to the assumption that the stack is not full. From postcondition: Stack gets updated to be non empty, w/ x on top, and count increased. ObligationsBenefits Client Supplier

Invariants Assertions that should always hold true In Eiffel, invariants have a class-wide scope: class MyStack[G] … invariant count <= capacity (count > 0) implies repr.item(count) = item)

Invariants (Sometimes) It’s unreasonable for invariants to always be true: Invariant: x != y swapping x and y would require 2 temporary variables, and some extra code When to suspend invariants? –obj.method(…) must satisfy on call and exit –method(…) need not (auxiliary tools)

Other Features of DBC Checkpoints –Much like C assert statements: check not s.empty end Loop invariants and variants –Off by 1, failure to terminate, border cases –Don’t think it’s hard? –Binary searching is commonly buggy

Example Loop (gcd) from x:= a; y:= b invariant -- optional x>0;y>0 variant -- optional x.max(y) until x = y loop if x > y then x := x - y else y := y - x end end

Problems with DBC Misuse –Contracts are part of your “interface”. Yet they can depend on private data. –Use as a control structure –Use for user input checking –Method body tests for assertions –Failing to update assertions Limitations of the assertion language

Eiffel’s Assertion Language boolean expressions, + old, etc. No complex formal concepts ( ,  ) An engineering tradeoff: –Enough formal elements for reliability gains –Yet, keep it simple, learnable and efficient