Design by Contract Fall 2016 Version.

Slides:



Advertisements
Similar presentations
Addressing the Challenges of Current Software. Questions to Address Why? What? Where? How?
Advertisements

Utilities (Part 3) Implementing static features 1.
Chapter 8 Designing Classes. Assignment Chapter 9 Review Exercises (Written)  R8.1 – 8.3, 8.5 – 8.7, 8. 10, 8.11, 8.13, 8.15, 8.19, 8.20 Due Friday,
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
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.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
CS Fall 2007 Dr. Barbara Boucher Owens. CS 2 Text –Main, Michael. Data Structures & Other Objects in Java Third Edition Objectives –Master building.
Writing JavaDocs Mimi Opkins CECS 274 Copyright (c) Pearson All rights reserved.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
“The greatest crimes do not arise from a want of feeling for others but from an over-sensibility for ourselves and an over-indulgence to our own desires.”
Data Structures Chapter 1- Introduction Mohamed Mustaq.A.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Utilities (Part 2) Implementing static features 1.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Exceptions and assertions CSE 331 University of Washington.
© Paul Ammann, 2008 Design by Contract Paul Ammann CS/SWE 332.
1  lecture slides online
Class Design I Class Contracts Readings: 2 nd Ed: Section 9.5, Advanced Topic nd Ed: Section 8.5, Advanced Topic 8.2 Some ideas come from: “Practical.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
CSE 1030: Implementing Static Features Mark Shtern.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
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.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Variable Scope & Lifetime
Modular Decomposition, Abstraction and Specifications
More Sophisticated Behavior
Chapter 6 CS 3370 – C++ Functions.
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Chapter 8 – Software Testing
CSE 374 Programming Concepts & Tools
Software Development Handing Errors and Creating Documentation
Testing UW CSE 160 Winter 2017.
About the Presentations
CSE 143 Error Handling [Section 2.8] 3/30/98 CSE 143.
Stack Data Structure, Reverse Polish Notation, Homework 7
Classes and Objects 2nd Lecture
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Preconditions, Postconditions & Assertions
Higher-Order Procedures
Announcements General rules about homeworks
Testing UW CSE 160 Winter 2016.
slides created by Ethan Apter
Algorithm Efficiency, Big O Notation, and Javadoc
Slides by Steve Armstrong LeTourneau University Longview, TX
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.
Arrays in Java What, why and how Copyright Curt Hill.
Effective Java, 3rd Edition Chapter 10: Exceptions
slides created by Ethan Apter
Defining Classes and Methods
Announcements Homework 1 will be assigned this week,
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
7 Arrays.
Chapter 18 Recursion.
Announcements General rules about homeworks
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Object/Class Design.
Effective Java, Chapter 9: Exceptions
Computer Science 340 Software Design & Testing
slides created by Ethan Apter and Marty Stepp
Software Specifications
Formal Methods Lecture 16 March 22, 2011 CS 315 Spring 2011
Presentation transcript:

Design by Contract Fall 2016 Version

Motivation for Modular Design Separation of concerns Independent development by team members Bug localization Ease of evolution and maintenance

Design by Contract (DBC) Also known as programming-to-the-interface Articulated clearly only in the 1980s Design-by-contract has become the standard policy governing separation of concerns across modern software engineering This is how software components are really used…

Idea of a Contract There are component providers (or implementers) and users (or clients) Implementers and clients communicate through contracts Contracts “hide information” by design, because they are irrelevant for clients Contracts may be formal or informal, but they must be understandable to clients so hidden info cannot be used!

Contract for an Operation A requires clause (precondition) that characterizes the responsibility of the caller Not all operations will have a requires clause. An ensures clause (postcondition) that characterizes the responsibility of the code that implements that operation Implementer assumes the requires clause. Ensures clause is guaranteed only if the caller satisfies the requires clause!

Why Use Ensures Clauses? Without them, how can a user know what the operation does? Names alone can’t tell the whole story! More on this later. Don’t want users to have to look at code! Defeats the very idea of using design by contract.

Why Use Requires Clauses? Efficiency! In the absence of requires clauses, implementations will have to do error checking. Those checks will happen every time an operation is called, whether or not checks are needed!

Simple Example Example function private static int func (int x) {…} Suppose for func to work properly x must be between 1 and 4. If the code for func did the checking, it would perform a check even on the following call! int ans = func(3); For the above call, neither the caller nor the code needs to do any checking!

Simple Example Example function private static int func (int x) {…} Suitable requires clause for func 1 <= x <= 4

1. Contract: Requires clause Write a suitable requires clause for the factorial function below. private static long factorial(short i) {… }

Another Example Operation (or method) to binary search if an item is in an array Requires that the array is sorted Ensures that it returns true if the item is in the array and false otherwise Why should the search code not check the requires clause?

Another Example Operation (or method) to binary search if an item is in an array Requires that the array is sorted Ensures that it returns true if the item is in the array and false otherwise Why should the search code not check the requires clause? It would take linear time to check that the array is sorted, though it’d take only log time to search!

Yet Another Example Integer division operation Does it have an (implicit) requires clause? Why?

Yet Another Example Integer division operation Does it have an (implicit) requires clause? Why? Yes, divisior should not be 0! Division operator doesn’t check, because then divisions would be slow! So it’s the division user’s responsibility, a requires clause!

2. Contract: Requires clause Function compute is going to access the array “a” at “index” and do some computation. Write a suitable requires clause. private static int compute (int[] a, int index) {… }

Last little bit In general, requires clauses avoid unnecessary code and improve code efficiency However, end users may not understand responsibilities such as requires clauses, so error checking needs to be coded! Example: User, input a choice between 1 and 10.

Role of Contracts What does this method call do? How do you know? double a, b; … double c = sqrt (a*a + b*b, 0.001); 12 November 201812 November 2018 OSU CSE

Example of a Contract /** * ... * @param x number to take the square root of * @param epsilon allowed relative error * @return the approximate square root of x * @requires * x > 0  and  epsilon > 0  * @ensures <pre>  * sqrt >= 0  and  * [sqrt is within relative error epsilon * of the actual square root of x]  * </pre>  */ private static double sqrt(double x, double epsilon) 12 November 201812 November 2018 OSU CSE

Example of a Contract A Java comment that starts with the symbols /** * ... * @param x number to take the square root of * @param epsilon allowed relative error * @return the approximate square root of x * @requires * x > 0  and  epsilon > 0  * @ensures <pre>  * sqrt >= 0  and  * [sqrt is within relative error epsilon * of the actual square root of x]  * </pre>  */ private static double sqrt(double x, double epsilon) A Java comment that starts with the symbols /** is called a Javadoc comment; it goes before the method header. 12 November 201812 November 2018 OSU CSE

Javadoc The standard documentation technique for Java is called Javadoc You place special Javadoc comments enclosed in /** … */ in your code, and the javadoc tool generates nicely formatted web-based documentation from them 12 November 201812 November 2018 OSU CSE

APIs The resulting documentation is known as the API (application programming interface) for the Java code to which the Javadoc tags are attached Example API: OSU CSE components at: http://cse.osu.edu/software/common/doc/ 12 November 201812 November 2018 OSU CSE

The word interface has two related but distinct meanings: APIs The resulting documentation is known as the API (application programming interface) for the Java code to which the Javadoc tags are attached The word interface has two related but distinct meanings: a unit of Java code that contains Javadoc comments used to produce documentation the resulting documentation 12 November 201812 November 2018 OSU CSE

Example of a Contract /** * ... * @param x number to take the square root of * @param epsilon allowed relative error * @return the approximate square root of x * @requires * x > 0  and  epsilon > 0  * @ensures <pre>  * sqrt >= 0  and  * [sqrt is within relative error epsilon * of the actual square root of x]  * </pre>  */ private static double sqrt(double x, double epsilon) The Javadoc tag @param is needed for each formal parameter; you describe the parameter’s role in the method. 12 November 201812 November 2018 OSU CSE

Example of a Contract /** * ... * @param x number to take the square root of * @param epsilon allowed relative error * @return the approximate square root of x * @requires * x > 0  and  epsilon > 0  * @ensures <pre>  * sqrt >= 0  and  * [sqrt is within relative error epsilon * of the actual square root of x]  * </pre>  */ private static double sqrt(double x, double epsilon) The Javadoc tag @return is needed if the method returns a value; you describe the returned value. 12 November 201812 November 2018 OSU CSE

Example of a Contract The Javadoc tag @requires introduces the precondition for the sqrt method. /** * ... * @param x number to take the square root of * @param epsilon allowed relative error * @return the approximate square root of x * @requires * x > 0  and  epsilon > 0  * @ensures <pre>  * sqrt >= 0  and  * [sqrt is within relative error epsilon * of the actual square root of x]  * </pre>  */ private static double sqrt(double x, double epsilon) 12 November 201812 November 2018 OSU CSE

Example of a Contract The Javadoc tag @ensures introduces the postcondition for the sqrt method. /** * ... * @param x number to take the square root of * @param epsilon allowed relative error * @return the approximate square root of x * @requires * x > 0  and  epsilon > 0  * @ensures <pre>  * sqrt >= 0  and  * [sqrt is within relative error epsilon * of the actual square root of x]  * </pre>  */ private static double sqrt(double x, double epsilon) 12 November 201812 November 2018 OSU CSE

Example of a Contract Javadoc comments may contain HTML-like tags; e.g., <pre> … </pre> means spacing and line-breaks are retained in generated documentation. /** * ... * @param x number to take the square root of * @param epsilon allowed relative error * @return the approximate square root of x * @requires * x > 0  and  epsilon > 0  * @ensures <pre>  * sqrt >= 0  and  * [sqrt is within relative error epsilon * of the actual square root of x]  * </pre>  */ private static double sqrt(double x, double epsilon) 12 November 201812 November 2018 OSU CSE

3. Contract Basics Which of the following statements are true? Ensures clause is the responsibility of the operation (method) implementer Requires clause is assumed by the implementer before the code Requires clause is the responsibility of the caller Ensures clause is assumed by the caller after the call

Abbreviated Javadoc For this course: Code you see in these slides will not have the Javadoc tags @param, @return, and formatting tags <pre>; plus, “keywords” in the Javadoc and mathematics will be bold-faced for easy reading This allows you to focus on the contract content: the requires and ensures clauses themselves 12 November 201812 November 2018 OSU CSE

Example Contract (Abbreviated) /** * ... * @requires * x > 0  and  epsilon > 0  * @ensures  * sqrt >= 0  and  * [sqrt is within relative error epsilon * of the actual square root of x]  */ private static double sqrt(double x, double epsilon) 12 November 201812 November 2018 OSU CSE

Example Contract (Abbreviated) /** * ... * @requires * x > 0  and  epsilon > 0  * @ensures  * sqrt >= 0  and  * [sqrt is within relative error epsilon * of the actual square root of x] }  */ private static double sqrt(double x, double epsilon) This is the precondition, indicating that the arguments passed in for the formal parameters x and epsilon both must be positive before a client may call sqrt. 12 November 201812 November 2018 OSU CSE

Example Contract (Abbreviated) /** * ... * @requires * x > 0  and  epsilon > 0  * @ensures  * sqrt >= 0  and  * [sqrt is within relative error epsilon * of the actual square root of x] }  */ private static double sqrt(double x, double epsilon) The precondition is a statement about the models of the arguments; here, it is a formal mathematical statement about mathematical reals. 12 November 201812 November 2018 OSU CSE

Example Contract (Abbreviated) /** * ... * @requires * x > 0  and  epsilon > 0  * @ensures  * sqrt >= 0  and  * [sqrt is within relative error epsilon * of the actual square root of x]  */ private static double sqrt(double x, double epsilon) This is the postcondition, indicating that the return value from sqrt is non-negative and … what does the rest say? 12 November 201812 November 2018 OSU CSE

Example Contract (Abbreviated) /** * ... * @requires * x > 0  and  epsilon > 0  * @ensures  * sqrt >= 0  and  * [sqrt is within relative error epsilon * of the actual square root of x]  */ private static double sqrt(double x, double epsilon) The first part of the postcondition here is written in mathematical notation; it is not program code! The second part — inside […] — is written in English. 12 November 201812 November 2018 OSU CSE

Using a Method Contract A static method’s contract refers to its formal parameters, and (only if it returns a value, not void) to the name of the method (which stands for the return value) To determine whether the precondition and postcondition are true for a particular client call: The values of the arguments are substituted for the respective formal parameters The value of the result returned by the method is substituted for the method name 12 November 201812 November 2018 OSU CSE

Reasoning: Tracing Tables Code State y = 76.9 y = sqrt(4.0, 0.01); y = 2.0 12 November 201812 November 2018 OSU CSE

Reasoning: Tracing Tables Code State y = 76.9 z = 4.0 y = sqrt(z, 0.01); y = 2.0 12 November 201812 November 2018 OSU CSE

Reasoning: Tracing Tables From the contract of sqrt, do we know that y = 2.0 instead of y = –2.0? Code State y = 76.9 z = 4.0 y = sqrt(z, 0.01); y = 2.0 12 November 201812 November 2018 OSU CSE

Reasoning: Tracing Tables From the contract of sqrt, do we know that y = 2.0 instead of y = 1.9996? Code State y = 76.9 z = 4.0 y = sqrt(z, 0.01); y = 2.0 12 November 201812 November 2018 OSU CSE

A Partly Informal Contract /** * ... * @requires * x > 0  and  epsilon > 0  * @ensures  * sqrt >= 0  and  * [sqrt is within relative error epsilon * of the actual square root of x]  */ private static double sqrt(double x, double epsilon) 12 November 201812 November 2018 OSU CSE

A Formal Contract /** * ... * @requires * x > 0 and epsilon > 0  * @ensures  * sqrt >= 0  and  * |sqrt - x^(1/2)| / x^(1/2) <= epsilon  */ private static double sqrt(double x, double epsilon) 12 November 201812 November 2018 OSU CSE

A Formal Contract We can, in this formal setting, easily substitute 4.0 for x, 0.01 for epsilon, and either 2.0 or 1.9996 for sqrt … and is the ensures clause is true in either case? Yes! /** * ... * @requires * x > 0  and  epsilon > 0  * @ensures  * sqrt >= 0  and  * |sqrt - x^(1/2)| / x^(1/2) <= epsilon  */ private static double sqrt(double x, double epsilon) 12 November 201812 November 2018 OSU CSE

Using contracts for reasoning There are reasoning tools that can leverage formal contracts The tools can check that client code does not violate preconditions The tools can check implementation code does not violate postconditions You will be exposed to one such tool in this course

Using Java “assert” statements To localize bugs during development and debugging in Java, we can use “assert” statements in client or implementation code Example: assert x > 0.0 : “Violation of precondition x > 0”; This checking can be turned off (to avoid inefficiency in production software) using the “-ea” argument to the JVM

Resources Wikipedia: Design by Contract http://en.wikipedia.org/wiki/Design_by_contract