CSE 331 Software Design and Implementation

Slides:



Advertisements
Similar presentations
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 6: Reasoning about Data Abstractions.
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES II Autumn 2011.
Computer Science 340 Software Design & Testing Design By Contract.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
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,
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
Reasoning about programs March CSE 403, Winter 2011, Brun.
Data Abstractions EECE 310: Software Engineering.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Abstract Data Types – Examples / Summary (Based on slides by Mike Ernst and David Notkin)
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
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.
Representation invariants and abstraction functions CSE 331 UW CSE.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Abstraction Functions (Based on slides by Mike Ernst, David Notkin, Hal Perkins)
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES Autumn 2011.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 7: A Tale of Two Graphs (and.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 6 Representation Invariants.
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.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst and David.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
EECE 310: Software Engineering
EECE 310: Software Engineering
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
CSE 374 Programming Concepts & Tools
CSE 331 Software Design and Implementation
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Specifications Liskov Chapter 9
Testing UW CSE 160 Spring 2018.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Design by Contract Fall 2016 Version.
CSE 331 Software Design and Implementation
CSE 331 Software Design and Implementation
CSE373: Data Structures & Algorithms Lecture 25: Software-Design Interlude – Preserving Abstractions Catie Baker Spring 2015.
Chapter 6 Methods: A Deeper Look
Testing UW CSE 160 Winter 2016.
CSE 331 Software Design & Implementation
Equality, conclusion Based on material by Michael Ernst, University of Washington.
CSE 331 Software Design and Implementation
Data Abstraction David Evans cs205: engineering software
CSE373: Data Structures & Algorithms Lecture 16: Software-Design Interlude – Preserving Abstractions Dan Grossman Fall 2013.
Lecture 7: A Tale of Two Graphs CS201j: Engineering Software
Lecture 4: Data Abstraction CS201j: Engineering Software
CSE 303 Concepts and Tools for Software Development
CSE 331 Software Design & Implementation
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,
CSE 331 Software Design and Implementation
slides created by Marty Stepp
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
CS2013 Lecture 7 John Hurley Cal State LA.
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
CSE 331 Software Design & Implementation
Computer Science 340 Software Design & Testing
CMPE212 – Reminders Assignment 2 due next Friday.
Software Specifications
slides created by Marty Stepp
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

CSE 331 Software Design and Implementation Lecture 5 Representation Invariants Leah Perlmutter / Summer 2018

Announcements

Announcements Happy Friday! My t-shirt Next week HW2 due Monday, July 4 at 10pm It’s harder than HW1 Please take advantage of today’s office hours! July 4 holiday on Wednesday -- no class! TAs will announce OH changes HW3 due Thursday

Motivation

Review Method Specification Abstract Data Type (abstraction) lec04 lec05 IMPLEMENTS IMPLEMENTS An ADT is a kind of specification, for data a method spec can have multiple implementations that all meet the spec an ADT can have multiple implementations that satisfy the data abstraction Method Body (concrete code) Data Structure (concrete code)

An ADT is a specification Abstract state + collection of procedural abstractions (aka method specs) Not a collection of procedures Together, these procedural abstractions provide some set of values All the ways of directly using that set of values Creating Manipulating Observing Creators and producers: make new values Mutators: change the value Observers: allow one to distinguish different values

An ADT has an abstract value Abstract Value: An Int List is a finite sequence of integer values Integer(1) Integer(2) Integer(42) Integer(17) null size: 4 head Integer(1) Integer(2) Integer(42) Integer(17) null size: 3 head null Integer(2) Integer(42) Integer(17) size: 0 head - Let’s say we implement the Int List ADT using a linked list implementation Implementation has a size and a pointer to head, which is the first node in the linked list 1. What is the abstract value of this list? 2. This list has size 3, but 4 elements. What is the abstract value of this list? Please give me a wrong answer and explain why it’s wrong. Notion of Internal Consistency 3. There are a few problems with this list. Notion of allowed values 1, 2, 42, 17 ? ?????

ADTs and specs Values allowed by the Data Structure (concrete values) Valid ADT Values (abstract values) Gray area of invalid values Today’s lecture will give us intellectual tools for thinking about the gray area and implementing data structures so they don’t accidentally take on values in the gray area - from producers - from value in ADT + operation

ADTs and specifications lec05 So far, we have only specified ADTs Specification makes no reference to the implementation Of course, we need [guidelines for how] to implement ADTs Data Structure implements an ADT Of course, we need [guidelines for how] to ensure our implementations satisfy our specifications Two intellectual tools are really helpful…

Connecting implementations to specs lec06 (today) Representation Invariant: maps Object → boolean Indicates if an instance is well-formed Defines the set of valid concrete values Only values in the valid set make sense as implementations of an abstract value For implementors/debuggers/maintainers of the abstraction: no object should ever violate the rep invariant Such an object has no useful meaning Abstraction Function: maps Object → abstract value What the data structure means as an abstract value How the data structure is to be interpreted Only defined on objects meeting the rep invariant For implementors/debuggers/maintainers of the abstraction: Each procedure should meet its spec (abstract values) by “doing the right thing” with the concrete representation lec07 - These two are related If an object is well formed and satisfies the rep invariant, then I can tell you its abstract value If it is poorly formed, like our circular linked list, then I can’t tell you its abstract value, it doesn’t have one

Representation Invariants

Implementing a Data Abstraction (ADT) To implement a data abstraction: Select the representation of instances, “the rep” In Java, typically instances of some class you define Implement operations in terms of that rep Choose a representation so that: It is possible to implement required operations The most frequently used operations are efficient But which will these be? Abstraction allows the rep to change later So for example with our linked list, the representation consists of a Size field a head pointer that points to a chain of nodes each node has an integer and a pointer to next

Example: CharSet Abstraction // Overview: A CharSet is a finite mutable set of Characters // @effects: creates a fresh, empty CharSet public CharSet() {…} // @modifies: this // @effects: thispost = thispre + {c} public void insert(Character c) {…} // @effects: thispost = thispre - {c} public void delete(Character c) {…} // @return: (c  this) public boolean member(Character c) {…} // @return: cardinality of this public int size() {…} set – see Wolfram Alpha definition set union set difference We’re using the mathematical notion of a set union: put in or if c already in set, no change difference: remove or if c not in set, no change This means an element is in the set once or not at all no notion of ordering no notion of duplication Informal notation warning

An implementation: Is it right? class CharSet { private List<Character> elts = new ArrayList<Character>(); public void insert(Character c) { elts.add(c); } public void delete(Character c) { elts.remove(c); public boolean member(Character c) { return elts.contains(c); public int size() { return elts.size(); the rep When I say go, introduce yourself to your neighbor (you say your name and they say their name), and discuss: Does this implementation prevent poorly formed values? If you think that it allows poorly formed values, try to scribble down some code that puts it in a bad state. Circuation question Quick poll: yes/no/not sure & why Does this implementation prevent poorly formed values?

An implementation: Is it right? class CharSet { private List<Character> elts = new ArrayList<Character>(); public void insert(Character c) { elts.add(c); } public void delete(Character c) { elts.remove(c); public boolean member(Character c) { return elts.contains(c); public int size() { return elts.size(); CharSet s = new CharSet(); Character a = new Character('a'); s.insert(a); s.delete(a); if (s.member(a)) System.out.print("wrong"); else System.out.print("right");

An implementation: Is it right? class CharSet { private List<Character> elts = new ArrayList<Character>(); public void insert(Character c) { elts.add(c); } public void delete(Character c) { elts.remove(c); public boolean member(Character c) { return elts.contains(c); public int size() { return elts.size(); Where is the error? There is more than one answer to this question Please raise your hand if you can think of one possible answer Where is the error?

Where Is the Error? If you can answer this, then you know what to fix Perhaps delete is wrong Should remove all occurrences? Perhaps insert is wrong Should not insert a character that is already there? How can we know? The representation invariant tells us If it’s “our code”, this is how we document our choice for “the right answer”

The representation invariant Defines data structure well-formedness Must hold before and after every CharSet operation Operations (methods) may depend on it Write it like this: class CharSet { // Rep invariant: // this.elts has no nulls and no duplicates private List<Character> elts = … … Or, more formally (if you prefer):  indices i of this.elts , this.elts.elementAt(i) ≠ null  indices i, j of this.elts , this.elts.elementAt(i).equals(this.elts.elementAt(j))  i = j  = ”for all”  = logical implication

The representation invariant class CharSet { // Rep invariant: // this.elts has no nulls and no duplicates private List<Character> elts = … … } Written in terms of the representation (this.elts) Internal comment (not javadoc) located just inside of the class definition at the very beginning

The representation invariant class CharSet { // Rep Invariant: // for all indices i of this.elts , // this.elts.elementAt(i) is not null // for all indices i, j of this.elts , // this.elts.elementAt(i). // equals(this.elts.elementAt(j)) // implies that i = j private List<Character> elts = … … } Written in terms of the representation (this.elts) Internal comment (not javadoc) located just inside of the class definition at the very beginning You could also write it the math way I think in this case, the English way is more clear In some cases the math way might be more clear Either is acceptable More clear is better

Now we can locate the error // Rep invariant: // elts has no nulls and no duplicates public void insert(Character c) { elts.add(c); } public void delete(Character c) { elts.remove(c);

Another example class Account { private int balance; // history of all transactions private List<Transaction> transactions; … } Real-world constraints: Balance ≥ 0 Balance = Σi transactions.get(i).amount Implementation-related constraints: Transactions ≠ null No nulls in transactions the rep the rep Σi ... = the sum, over all values of i, of ... - Balance is the sum of all transactions

Checking rep invariants Should code check that the rep invariant holds? Short answer: YES!!!! More considerations Development vs. Production Always yes in development [even when it’s expensive] Production... usually yes (See Pragmatic Programmer: Assertive Programming) computational cost [depends on the invariant] sometimes no for expensive checks, in production Some private methods need not check (Why?) A great debugging technique: Design your code to catch bugs early by implementing and using rep-invariant checking Dev vs. Prod computational cost could be a reason not to checkRep is a great way to help you write code with fewer mistakes to do this, implement checkRep first!!! In fact, this technique is so valuable that you’ll be graded on whether you use it initially it seems like more work... implement this class AND its checkRep Actually, implementing checkRep first makes things easier helps you catch errors early, and the earlier you catch them, the easier they are to fix

Checking the rep invariant Rule of thumb: check on entry and on exit (why?) /** Verify elts has no nulls or duplicates and... */ private void checkRep() { for (int i = 0; i < elts.size(); i++) { assert elts.elementAt(i) != null; assert elts.indexOf(elts.elementAt(i)) == i; } ... // more checks public void delete(Character c) { checkRep(); elts.remove(c); // Is this guaranteed to get called? // (could guarantee it with a finally block) What does assert mean? Assume you will make mistakes!!!! When I was about 19, I didn’t realize it was ok to be wrong. Some time in college, about when I started interviewing for jobs, I realized I wasn’t always right, and didn’t have to be. After this realization, I felt that I had leveled up as an adult human This is really important in job interviews They don’t expect you to be always right (they set you up so you’re not) They do expect you to be resilient in dealing with the ways that you’re wrong So... Acknowledge that you will make mistakes because you are an imperfect human. Implement and call CheckRep! check on entry to make sure a client or another method hasn’t messed up the rep check on exit to make sure this method hasn’t messed up the rep

Practice defensive programming Assume that you will make mistakes Write and incorporate code designed to catch them On entry: Check rep invariant Check preconditions On exit: Check postconditions Checking the rep invariant helps you discover errors Reasoning about the rep invariant helps you avoid errors -

Summary so far... We implement an Abstract Data Type with a Data Structure Every Data Structure has a Representation that is a concrete way of representing an object’s abstract value The representation allows concrete values that do not correspond to an abstract value “gray area” --------------------------------- Representation Invariant describes what makes the concrete representation valid (green area) checkRep() method verifies that the rep is valid, throws exception if not, protects you from yourself Check your rep all the time! generally at beginning and end of every public method -

Representation Exposure

Listing the elements of a CharSet Consider adding the following method to CharSet // returns: a List containing the members of this public List<Character> getElts(); Consider this implementation: // Rep invariant: elts has no nulls and no dups public List<Character> getElts() { return elts; } Does the implementation of getElts preserve the rep invariant?

Listing the elements of a CharSet Consider adding the following method to CharSet // returns: a List containing the members of this public List<Character> getElts(); Consider this implementation: // Rep invariant: elts has no nulls and no dups public List<Character> getElts() { return elts; } Does the implementation of getElts preserve the rep invariant? Kind of, sort of, not really….

Representation exposure Consider this client code (outside the CharSet implementation): CharSet s = new CharSet(); Character a = new Character(’a’); s.insert(a); s.getElts().add(a); s.delete(a); if (s.member(a)) … Representation exposure is external access to the rep Representation exposure is almost always evil A big deal, a common bug, you now have a name for it! If you do it, document why and how And feel guilty about it!

Avoiding representation exposure Understand what representation exposure is Design ADT implementations to make sure it doesn’t happen Treat rep exposure as a bug: fix your bugs Test for it with adversarial clients: Pass values to methods and then mutate them Mutate values returned from methods

private is not enough Client code The Rep Abstraction Barrier Making fields private does not suffice to prevent rep exposure See our example So private is a hint to you: no aliases outside abstraction to references to mutable data reachable from private fields Two general ways to avoid representation exposure…

Avoiding rep exposure (way #1) One way to avoid rep exposure is to make copies of all data that cross the abstraction barrier Copy in [parameters that become part of the implementation] Copy out [results that are part of the implementation] Examples of copying (assume Point is a mutable ADT): class Line { private Point s, e; public Line(Point s, Point e) { this.s = new Point(s.x,s.y); this.e = new Point(e.x,e.y); } public Point getStart() { return new Point(this.s.x,this.s.y); … copy in copy out

Need deep copying “Shallow” copying is not enough Prevent any aliasing to mutable data inside/outside abstraction What’s the bug (assuming Point is a mutable ADT)? class PointSet { private List<Point> points = … public List<Point> getElts() { return new ArrayList<Point>(points); } Not in example: Also need deep copying on “copy in” client can’t access the arraylist of the rep can’t add or remove points CAN access the individual points in the rep can modify them by calling mutator methods copy out?

Avoiding rep exposure (way #2) One way to avoid rep exposure is to exploit the immutability of (other) ADTs the implementation uses Aliasing is no problem if nobody can change data Have to mutate the rep to break the rep invariant Examples (assuming Point is an immutable ADT): class Line { private Point s, e; public Line(Point s, Point e) { this.s = s; this.e = e; } public Point getStart() { return this.s; …

Why [not] immutability? Several advantages of immutability Aliasing does not matter No need to make copies with identical contents Rep invariants cannot be broken Take CSE 341: Programming Languages for more! Does require different designs (e.g., if Point immutable) void raiseLine(double deltaY) { this.s = new Point(s.x, s.y+deltaY); this.e = new Point(e.x, e.y+deltaY); } Immutable classes in Java libraries include String, Character, Integer, … pro: client can’t modify rep con: nobody can modify the rep Actually immutability is really good in many cases (See EJ: Minimize Mutability)

Deepness revisited An immutable ADT must be immutable “all the way down” No references reachable to data that may be mutated So combining our two ways to avoid rep exposure: Must copy-in, copy-out “all the way down” to immutable parts

Back to getElts Recall our initial rep-exposure example: class CharSet { // Rep invariant: elts has no nulls and no dups private List<Character> elts = …; // returns: elts currently in the set public List<Character> getElts() { return new ArrayList<Character>(elts); } … } copy out!

An alternative // returns: elts currently in the set public List<Character> getElts() { // version 1 return new ArrayList<Character>(elts);//copy out! } public List<Character> getElts() { // version 2 return Collections.unmodifiableList<Character>(elts); From the JavaDoc for Collections.unmodifiableList: Returns an unmodifiable view of the specified list. This method allows modules to provide users with "read-only" access to internal lists. Query operations on the returned list "read through" to the specified list, and attempts to modify the returned list… result in an UnsupportedOperationException. this works if we have immutable points Can return a list of them that the client can’t modify

The good news public List<Character> getElts() { // version 2 return Collections.unmodifiableList<Character>(elts); } Clients cannot modify (mutate) the rep So they cannot break the rep invariant (For long lists,) more efficient than copy out Uses standard libraries

Caveat public List<Character> getElts() { // version 1 return new ArrayList<Character>(elts);//copy out! } public List<Character> getElts() { // version 2 return Collections.unmodifiableList<Character>(elts); The two implementations do not do the same thing! Both avoid allowing clients to break the rep invariant Both return a list containing the elements But consider: xs = s.getElts(); s.insert('a'); xs.contains('a'); Version 2 is observing an exposed rep, leading to different behavior Different behavior from copying out - explain to neighbor

Different specifications Ambiguous spec: “returns a list containing the current set elements” 1. “returns a fresh mutable list containing the elements in the set at the time of the call” versus 2. “returns read-only access to a list that the ADT continues to update to hold the current elements in the set” 3. A third spec weaker than both [but less simple and useful!] “returns a list containing the current set elements. Behavior is unspecified (!) if client attempts to mutate the list or to access the list after the set’s elements are changed” Also note: Version 2’s spec also makes changing the rep later harder Only “simple” to implement with rep as a List

Summary A data structure’s representation allows concrete values that do not correspond to an abstract value allowed by the ADT “gray area” ------- Representation Invariant describes what makes the concrete representation valid (green area) checkRep() method verifies that the rep is valid Rep Exposure occurs when a client can modify the rep Never let this happen!!!! -

Closing

Closing Announcements HW2 due Monday 10 pm Start early! Go to office hours today! Thank you for coming to class! Enjoy your weekend!