CSE 331 Software Design & Implementation specifications

Slides:



Advertisements
Similar presentations
1 More on Arrays Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 – 7.2 Reading for this lecture:
Advertisements

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.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
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/
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Specifications (Slides by Mike Ernst) 1.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Lecture 4 - Specifications (Based on slides by Mike Ernst, Hal Perkins)
1 CSE 331 Specifications slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Variable Scope & Lifetime
More on Arrays Review of Arrays of ints, doubles, chars
Modular Decomposition, Abstraction and Specifications
Using the Java Collection Libraries COMP 103 # T2
Review Array Array Elements Accessing array elements
Lists Rem Collier Room A1.02
EECE 310: Software Engineering
More Sophisticated Behavior
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Learning to Program D is for Digital.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Input Space Partition Testing CS 4501 / 6501 Software Testing
Chapter 11 Object-Oriented Design
Java Programming Language
CSE 374 Programming Concepts & Tools
Reasoning about code CSE 331 University of Washington.
Queues Queues Queues.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Specifications Liskov Chapter 9
CSE 331 Software Design & Implementation
Array.
Chapter 17 Object-Oriented Data Structures
Introduction to javadoc
Design by Contract Fall 2016 Version.
CSE 331 Software Design and Implementation
Object Oriented Programming (OOP) LAB # 8
Preconditions, Postconditions & Assertions
CSE373: Data Structures & Algorithms Lecture 25: Software-Design Interlude – Preserving Abstractions Catie Baker Spring 2015.
Introduction to Data Structures
Stacks.
slides created by Ethan Apter
Specifications, conclusion. Abstract Data Types (ADTs)
Algorithm Efficiency, Big O Notation, and Javadoc
Specifications.
CSE 331 Software Design & Implementation
CSE373: Data Structures & Algorithms Lecture 16: Software-Design Interlude – Preserving Abstractions Dan Grossman Fall 2013.
slides created by Ethan Apter
slides created by Alyssa Harding
Data Structures and Algorithms for Information Processing
Stacks.
Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract, Computer,
ArrayLists 22-Feb-19.
Introduction to javadoc
Introduction to Data Structure
List Implementation via Arrays
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.
Java Modeling Language (JML)
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Generics, Stack, Queue Based on slides by Alyssa Harding
Computer Science 340 Software Design & Testing
slides created by Ethan Apter and Marty Stepp
slides created by Ethan Apter
Software Specifications
slides created by Marty Stepp
Design Contracts and Errors A Software Development Strategy
Presentation transcript:

CSE 331 Software Design & Implementation specifications Shoes must be worn Dogs must be carried CSE 331 Software Design & Implementation specifications Michael Jackson Autumn 2011

The challenge of scaling software Package P Class A Method x Method y Class B Method z Small programs tend to be simple and malleable: relatively easy to write and to change Big programs tend to be complex and inflexible: harder to write and (much) harder to change Why? In large part because interactions become harder to understand and to manage We will try to reduce this challenge by using specifications to simplify and manage these interactions More classes, more methods, more calls, more generics, more imports, more inherits, more libraries, more static and private and public, … CSE 331 Autumn 2011

A specification is a contract A set of obligations agreed to by the user (client) and the manufacturer (implementer) of the product 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 Allows either side to make changes that respect the specification An effective specification changes very little (at most), allowing the code (on both sides) to be more malleable CSE 331 Autumn 2011

Different but dualistic roles Implementers vs. Clients Implementation: only must satisfy specification public interface Queue<E> { public void enqueue(E value); // post: given value inserted at the end of the queue // pre : !isEmpty() // post: removes and returns the value at the front of the queue public E dequeue(); // post: returns true if the queue is empty, false otherwise public boolean isEmpty(); // post: returns the current number of elements in the queue public int size(); } Specification public E dequeue(); // post: returns true if the queue is empty, false otherwise public boolean isEmpty(); // post: given value inserted at the end of the queue public interface Queue<E> { public void enqueue(E value); // pre : !isEmpty() // post: removes and returns the value at the front of the queue // post: returns the current number of elements in the queue public int size(); } public interface Queue<E> { Client code: must depend only on specification CSE 331 Autumn 2011

Respecting the specification has value If an implementation focuses on the needs of a specific client rather than only ensuring that the specification is satisfied, what happens to other clients? To the implementation itself? If a client uses properties of the implementation that are not part of the specification, what happens if the implementation changes those properties? Specification CSE 331 Autumn 2011

Leading towards “Truth, Justice and the 331 Way” You play both roles Not only in 331, but commonly in your career By reducing how much you and your dualistic “alter ego” know about each others’ view, the interactions can be kept cleaner This is hard! Leading towards “Truth, Justice and the 331 Way” CSE 331 Autumn 2011

Isn’t the interface a specification? Java (and most languages) allow programs to define interfaces as a boundary between the implementations and the clients public interface List<E> { public int get(int); public void set(int, E); public void add(E); public void add(int, E); … public static boolean sub(List<T>, List<T>); } The interface is a weak kind of specification that provides the syntax, but nothing about the behavior and the effects This kind of contract says, “I’ll give you this and you’ll give me that, but ‘this’ and ‘that’ aren’t carefully defined” CSE 331 Autumn 2011

Why not just read code? T boolean sub(List<T> src, List<T> 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; In small groups, spend 1-2 minutes listing reasons why reading code would be a poor substitute for having a specification CSE 331 Autumn 2011

Code is complicated Much detail not needed by client – understanding every line of code is excessive and impractical Ex: Read all source code of Java libraries before using them? Client should care only what is in the specification, not what is in the code When a client sees the implementation code, subconscious dependencies arise and may be exploited Why is this bad? Why should you be especially concerned about this? CSE 331 Autumn 2011

An old Drabble cartoon: “Too easy!!! It’s ‘Who do we appreciate?’” Why not just run code? The client depends on what the implementation computes – what better way to find out than by seeing what it computes? If you run enough test inputs, you are forming a partial specification Ex: from many standardized tests “What is next in this sequence: 2, 4, 6, 8 …?” “What is next in this sequence: 100, 50, 25, 76, 38, 19, 58, 29, 88, …?” Problems with this approach are similar to those shown in the 1st lecture via specification jeopardy An old Drabble cartoon: “Too easy!!! It’s ‘Who do we appreciate?’” CSE 331 Autumn 2011

Which code details are essential? A lot of choices are made in writing code – some are essential while others are incidental – but which is which? Internal variable names? Algorithms used? Resource consumption (time, space, bandwidth, etc.)? Documentation? Etc.? Code invariably gets rewritten, making the distinction between essential and incidental crucial What properties can the client rely on over time? Which properties must the implementer preserve for the client’s code to work? Future optimizations, improved algorithms, bug fixes, etc.? Alternatively, what properties might the implementation change that would break the client code? There is no simple definition of this distinction, but it is captured in practice in every specification – again, your sensibilities about this issue with grow over time CSE 331 Autumn 2011

Comments With more comments on comments later on Comments can, and do, provide value if and when written carefully – and when kept up-to-date Many 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) { ... } This usually leaves ambiguity – for example, what if src and part are both empty lists? CSE 331 Autumn 2011

Improving the spec of sub() // Check whether “part” appears as a sub-sequence in “src” Needs additional clarification // a) src and part cannot be null // b) If src is empty list, always returns false // c) 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 needs to be 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... CSE 331 Autumn 2011

Further improving the spec of sub() A complicated description suggests poor design and rarely clarifies a specification Try to simplify rather than describe complexity Perlis: Simplicity does not precede complexity, but follows it.” Rewrite the specification of sub() more clearly and sensibly The “declarative” style of this specification is important Contrast to an operational style such as “This method scans the “src” list from beginning to end…” The mathematical flavor is not necessary, but it can help reduce ambiguity // returns true iff sequences A, B exist such that // src = A : part : B [“:” is sequence concatenation] CSE 331 Autumn 2011

Examples of specifications Javadoc “is a tool for generating API documentation in HTML format from doc comments in source code.” Get used to using it Javadoc conventions expect programs to provide method prototype – basically, the name of the method and the types of the parameters and of the return text description of method @param: description of what gets passed in @returns: description of what gets returned @throws: list of exceptions that may occur CSE 331 Autumn 2011

Example: Javadoc for String.contains tags in Java comments These are parsed and formatted by Javadoc Viewable in web browsers 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: 1.5 /** * Returns true if and only if this string contains the specified * sequence of char values. * * @param s the sequence to search for * @return true if this string contains <code>s</code>, false otherwise * @throws NullPointerException if <code>s</code> is <code>null</code> * @since 1.5 */ public boolean contains(CharSequence s) { return indexOf(s.toString()) > -1; } CSE 331 Autumn 2011

CSE 331 specifications (Javadoc is extensible) The precondition: constraints that hold before the method is called requires: spells out any obligations on client (if requires is not satisfied by a client, the implementation is unconstrained) 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 effects: gives guarantees on the final state of modified objects returns: describes return value CSE 331 Autumn 2011

Ex 1: Spec and an implementation static int test(List<T> 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 no other changes to lst returns position of element in lst that was oldelt and is now newelt static int test(List<T> 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; Don’t write this code! CSE 331 Autumn 2011

Ex 2: Spec and an implementation static List<Integer> listAdd(List<Integer> lst1, List<Integer> 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<Integer> listAdd(List<Integer> lst1, List<Integer> lst2) { List<Integer> res = new ArrayList<Integer>(); for(int i = 0; i < lst1.size(); i++) { res.add(lst1.get(i) + lst2.get(i)); } return res; } Don’t write this code! CSE 331 Autumn 2011

Ex 3: Spec and an implementation static void listAdd2(List<Integer> lst1, List<Integer> 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<Integer> lst1, List<Integer> lst2) { for (int i = 0; i < lst1.size(); i++) { lst1.set(i, lst1.get(i) + lst2.get(i)); } Don’t write this code! CSE 331 Autumn 2011

Ex 4: Spec and an implementation static void uniquify(List<Integer> lst) requires ??? modifies ??? effects ??? returns ??? static void uniquify(List<Integer> lst) { for (int i=0; i < lst.size()-1; i++) if (lst.get(i) == lst.get(i+1)) lst.remove(i); } Don’t write this code! In small groups, spend 1-2 minutes filling in the ??? in the specification above CSE 331 Autumn 2011

Ex: java.util.Arrays.binarySearch public static int binarySearch(int[] a,int key) Searches the specified array of ints for the specified value using the binary search algorithm. The array must be sorted (as by the sort method, above) prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found. Parameters: a- the array to be searched. key- the value to be searched for. Returns: index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). […long description…] CSE 331 Autumn 2011

Improved specification public static int binarySearch(int[] a,int key) requires: a is sorted in ascending order returns: some i such that a[i] = key if such an i exists, otherwise -1 Returning (-(insertion point)-1) is an invitation to bugs and confusion Consider: The designers had a reason; what was it, and what are the alternatives? We'll return to the topic of exceptions and special values in a later lecture CSE 331 Autumn 2011

Summary Properties of a specification The client relies only on the specification and on nothing (else) from the implementation The implementer provides everything in the specification and is otherwise unconstrained Overall, effective use of specifications leads to simpler and more flexible programs that have fewer bugs and cleaner dependencies CSE 331 Autumn 2011

Next steps Assignment 0 Assignment 1 Assignment 2 Lectures Due today 11:59PM Assignment 1 out later today due Wednesday (10/5) 11:59PM Assignment 2 out Wednesday (10/5) due in two parts part A on Friday (10/7) 11:59PM part B the following Wednesday (10/12) at 11:59PM Lectures Testing and Junit (M) Equality (W) Abstract data types I (F) Abstract data types II (M) CSE 331 Autumn 2011

?