CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Specifications (Slides by Mike Ernst) 1.

Slides:



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

11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Cs205: engineering software university of virginia fall 2006 Specifying Procedures David Evans
Addressing the Challenges of Current Software. Questions to Address Why? What? Where? How?
Identity and Equality Based on material by Michael Ernst, University of Washington.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION DEBUGGING Autumn 2011 Bug-Date: Sept 9, 1947.
Utilities (Part 3) Implementing static features 1.
Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
CSC 395 – Software Engineering Lecture 21: Overview of the Term & What Goes in a Data Dictionary.
Specifications. Announcements HW1 is due Friday To submit answers, commit Error in HW1: In problem 8, part 2 z = 0 should be z = MAX_INT 2.
Specifications Liskov Chapter 9 SWE 619 Last Updated Fall 2008.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, David Notkin,
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
Slides by Vinod Rathnam with material from Alex Mariakakis Kellen Donohue, David Mailhot, and Hal Perkins Midterm Review.
Computer Science 340 Software Design & Testing Design By Contract.
Procedure specifications CSE 331. Outline Satisfying a specification; substitutability Stronger and weaker specifications - Comparing by hand - Comparing.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
CS Fall 2007 Dr. Barbara Boucher Owens. CS 2 Text –Main, Michael. Data Structures & Other Objects in Java Third Edition Objectives –Master building.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Writing JavaDocs Mimi Opkins CECS 274 Copyright (c) Pearson All rights reserved.
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.
23-Oct-15 Abstract Data Types. 2 Data types A data type is characterized by: a set of values a data representation, which is common to all these values,
Documentation Dr. Andrew Wallace PhD BEng(hons) EurIng
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
© Paul Ammann, 2008 Design by Contract Paul Ammann CS/SWE 332.
The Software Development Process
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
Type Abstraction SWE Spring October 05Kaushik, Ammann Substitution Principle “In any client code, if supertype object is substituted.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
Understanding ADTs CSE 331 University of Washington.
CSC 213 – Large Scale Programming. Today’s Goal  Understand why testing code is important  Result of poor or no testing & embarrassment caused  Learn.
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Lecture 4 - Specifications (Based on slides by Mike Ernst, Hal Perkins)
CSE 1030: Implementing Static Features Mark Shtern.
1 CSE 331 Specifications slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Reasoning and Design (and Assertions). How to Design Your Code The hard way: Just start coding. When something doesn’t work, code some more! The easier.
Slides by Alex Mariakakis Section 5: Midterm Review.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Modular Decomposition, Abstraction and Specifications
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
CSE 374 Programming Concepts & Tools
Specifications Liskov Chapter 9
CSE 331 Software Design & Implementation
CSE 143 Error Handling [Section 2.8] 3/30/98 CSE 143.
Design by Contract Fall 2016 Version.
CSE 331 Software Design and Implementation
slides created by Ethan Apter
Specifications, conclusion. Abstract Data Types (ADTs)
Exam 1 Review.
Programming Languages 2nd edition Tucker and Noonan
Specifications.
CSE 331 Software Design & Implementation
slides created by Ethan Apter
IMPORTANT NOTE Some parts of these section slides deal with null ints. This was a mistake, as primitives cannot be null. These issues have been corrected.
CSE 331 Software Design & Implementation specifications
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Computer Science 340 Software Design & Testing
slides created by Ethan Apter and Marty Stepp
Software Specifications
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Specifications (Slides by Mike Ernst) 1

2 Goals of Software System Building Building the right system –Does the program meet the user’s needs? –Determining this is usually called validation Building the system right –Does the program meet the specification? –Determining this is usually called verification CSE 331: the second goal is the focus – creating a correctly functioning artifact –It’s surprisingly hard to specify, design, implement, test, and debug even simple programs 2

Where we are We’ve started to see how to reason about code We’ll build on those skills in many places: –Specification: What are we supposed to build? –Design: How do we decompose the job into manageable pieces? Which designs are “better”? –Implementation: Building code that meets the specification (and we know it because we can prove it!) –Testing: OK, we know it’s right, but is it? –Debugging: If it’s not, how do we systematically find the problems and fix them? –Maintain: How does the artifact adapt over time? –Documentation: What do we need to know to do these things? How/where do we write that down? (Comments, JavaDoc, UML(?), …) 3

The challenge of scaling software Small programs are simple and malleable –easy to write –easy to change Big programs are (often) complex and inflexible –hard to write –hard to change Why does this happen? –Because interactions become unmanageable How do we keep things simple and malleable? 4

A discipline of modularity Two ways to view a program: –The implementer's view (how to build it) –The client's view (how to use it) It helps to apply these views to program parts: –While implementing one part, consider yourself a client of any other parts it depends on –Try not to look at those other parts through an implementer's eyes –This helps dampen interactions between parts Formalized through the idea of a specification 5

A specification is a contract A set of requirements agreed to by the user and the manufacturer of the product –Describes their expectations of each other Facilitates simplicity by two-way isolation –Isolate client from implementation details –Isolate implementer from how the part is used –Discourages implicit, unwritten expectations Facilitates change –Reduces the “Medusa” effect: the specification, rather than the code, gets “turned to stone” by client dependencies 6

Isn’t the interface sufficient? The interface is to defines the boundary between the implementers and users: public interface List { public E get(int); public void set(int, E); public void add(E); public void add(int, E); … public static boolean sub(List, List ); } Interface provides the syntax But nothing about the behavior and effects 7

Why not just read code? boolean sub(List src, List part) { int part_index = 0; for (Object o : src) { if (o.equals(part.get(part_index))) { part_index++; if (part_index == part.size()) { return true; } } else { part_index = 0; } return false; } Why are you better off with a specification? 8

Code is complicated Code gives more detail than needed by client Understanding or even reading every line of code is an excessive burden –Suppose you had to read source code of Java libraries in order to use them –Same applies to developers of different parts of the libraries Client cares only about what the code does, not how it does it 9

Code is ambiguous Code seems unambiguous and concrete –But which details of code's behavior are essential, and which are incidental? Code invariably gets rewritten –Client needs to know what they can rely on What properties will be maintained over time? What properties might be changed by future optimization, improved algorithms, or just bug fixes? –Implementer needs to know what features the client depends on, and which can be changed 10

Comments are essential Most comments convey only an informal, general idea of what that the code does: // This method checks if “part” appears as a // sub-sequence in “src” boolean sub(List src, List part) {... } Problem: ambiguity remains –e.g. what if src and part are both empty lists? 11

From vague comments to specifications Properties of a specification: –The client agrees to rely only on information in the description in their use of the part. –The implementer of the part promises to support everything in the description otherwise is perfectly at liberty Sadly, much code lacks a specification –Clients often work out what a method/class does in ambiguous cases by simply running it, then depending on the results –This leads to bugs and to programs with unclear dependencies, reducing simplicity and flexibility 12

Recall the sublist example T boolean sub(List src, List part) { int part_index = 0; for (T elt : src) { if (elt.equals(part.get(part_index))) { part_index++; if (part_index == part.size()) { return true; } } else { part_index = 0; } return false; } 13

A more careful description of sub() // Check whether “part” appears as a // sub-sequence in “src”. needs to be given some caveats (why?): // * src and part cannot be null // * If src is empty list, always returns false. // * Results may be unexpected if partial matches // can happen right before a real match; e.g., // list (1,2,1,3) will not be identified as a // sub sequence of (1,2,1,2,1,3). or replaced with a more detailed description: // This method scans the “src” list from beginning // to end, building up a match for “part”, and // resetting that match every time that... 14

It’s better to simplify than to describe complexity A complicated description suggests poor design Rewrite sub() to be more sensible, and easier to describe: // returns true iff sequences A, B exist such that // src = A : part : B // where “:” is sequence concatenation boolean sub(List src, List part) Mathematical flavor is not (always) necessary, but can (often) help avoid ambiguity “Declarative” style is important – avoids reciting or depending on operational/implementation details 15

Sneaky fringe benefit of specs #1 The discipline of writing specifications changes the incentive structure of coding –rewards code that is easy to describe and understand –punishes code that is hard to describe and understand (even if it is shorter or easier to write) If you find yourself writing complicated specifications, it is an incentive to redesign –sub() code that does exactly the right thing may be slightly slower than a hack that assumes no partial matches before true matches – but cost of forcing client to understand the details is too high 16

Examples of specifications Javadoc –Sometimes can be daunting; get used to using it Javadoc convention for writing specifications –method prototype –text description of method –param: description of what gets passed in –returns: description of what gets returned –throws: list of exceptions that may occur 17

Example: Javadoc for String.contains public boolean contains(CharSequence s) Returns true if and only if this string contains the specified sequence of char values. Parameters: s - the sequence to search for Returns: true if this string contains s, false otherwise Throws: NullPointerException Since:

CSE 331 specifications The precondition: constraints that hold before the method is called (if not, all bets are off) –requires: spells out any obligations on client The postcondition: constraints that hold after the method is called (if the precondition held) –modifies: lists objects that may be affected by method; any object not listed is guaranteed to be untouched –throws: lists possible exceptions (Javadoc uses this too) –effects: gives guarantees on the final state of modified objects –returns: describes return value (Javadoc uses this too) 19

Example 1 static int test(List lst, T oldelt, T newelt) requires lst, oldelt, and newelt are non-null. oldelt occurs in lst. modifies lst effects change the first occurrence of oldelt in lst to newelt & makes no other changes to lst returns the position of the element in lst that was oldelt and is now newelt static int test(List lst, T oldelt, T newelt) { int i = 0; for (T curr : lst) { if (curr == oldelt) { lst.set(newelt, i); return i; } i = i + 1; } return -1; } 20

Example 2 static List listAdd(List lst1, List lst2) requires lst1 and lst2 are non-null. lst1 and lst2 are the same size. modifies none effects none returns a list of same size where the ith element is the sum of the ith elements of lst1 and lst2 static List listAdd(List lst1 List lst2) { List res = new ArrayList (); for(int i = 0; i < lst1.size(); i++) { res.add(lst1.get(i) + lst2.get(i)); } return res; } 21

Example 3 static void listAdd2(List lst1, List lst2) requires lst1 and lst2 are non-null. lst1 and lst2 are the same size modifies lst1 effects ith element of lst2 is added to the ith element of lst1 returns none static void listAdd2(List lst1, List lst2) { for(int i = 0; i < lst1.size(); i++) { lst1.set(i, lst1.get(i) + lst2.get(i)); } 22

Should requires clause be checked? If the client calls a method without meeting the precondition, the code is free to do anything, including pass corrupted data back –It is polite, nevertheless, to fail fast: to provide an immediate error, rather than permitting mysterious bad behavior Preconditions are common in “helper” methods/classes –In public libraries, it’s friendlier to deal with all possible input –Example: binary search would normally impose a pre- condition rather than simply failing if list is not sorted. Why? Rule of thumb: Check if cheap to do so –Ex: list has to be non-null  check –Ex: list has to be sorted  skip 23

Comparing specifications Occasionally, we need to compare different versions of a specification (Why?) –For that, we talk about “weaker” and “stronger” specifications A weaker specification gives greater freedom to the implementer –If specification S 1 is weaker than S 2, then for any implementation I, I satisfies S 2 => I satisfies S 1 but the opposite implication does not hold in general 24

Example 1 int find(int[] a, int value) { for (int i=0; i<a.length; i++) { if (a[i]==value) return i; } return -1; } specification A –requires: value occurs in a –returns: i such that a[i] = value specification B –requires: value occurs in a –returns: smallest i such that a[i] = value 25

Example 2 int find(int[] a, int value) { for (int i=0; i<a.length; i++) { if (a[i]==value) return i; } return -1; } specification A –requires: value occurs in a –returns: i such that a[i] = value specification C –returns: i such that a[i]=value, or -1 if value is not in a 26

Stronger and weaker specifications A stronger specification is –Harder to satisfy (harder to implement) –Easier to use (more guarantees, more predictable) A weaker specification is –Easier to satisfy (easier to implement, more implementations satisfy it) –Harder to use (makes fewer guarantees) 27

Strengthening a specification strengthen a specification by: –promising more effects clause harder to satisfy, and/or fewer objects in modifies clause –asking less of client requires clause easier to satisfy weaken a specification by: –promising less effects clause easier to satisfy, and/or extra objects in modifies clause –asking more of the client requires clause harder to satisfy 28

Choosing specifications There can be different specifications for the same implementation! –Specification says more than implementation does –Declares which properties are essential – the method itself leaves that ambiguous –Clients know what they can rely on, implementers know what they are committed to Which is better : a strong or a weak specification? –It depends! –Criteria: simple, promotes reuse & modularity, efficient 29

Sneaky fringe benefit of specs #2 Specification means that client doesn't need to look at implementation –So the code may not even exist yet! Write specifications first, make sure system will fit together, and then assign separate implementers to different modules –Allows teamwork and parallel development –Also helps with testing, as we'll see shortly 30