Cs2220: Engineering Software Class 4: Specifying Procedures Fall 2010 University of Virginia David Evans.

Slides:



Advertisements
Similar presentations
Cs205: engineering software university of virginia fall 2006 Specifying Procedures David Evans
Advertisements

David Evans CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.
Cs2220: Engineering Software Class 10: Generic Datatypes Fall 2010 University of Virginia David Evans.
INTRODUCTION Chapter 1 1. Java CPSC 1100 University of Tennessee at Chattanooga 2  Difference between Visual Logic & Java  Lots  Visual Logic Flowcharts.
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Utilities (Part 3) Implementing static features 1.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
From Module Breakdown to Interface Specifications Completing the architectural design of Map Schematizer.
Code Clarity - Comments, Preconditions and Postconditions Cmput Lecture 2 Department of Computing Science University of Alberta ©Duane Szafron 1999.
Yixin Sun Class 17: Concurrency and OOP Fall 2010 UVa David Evans cs2220: Engineering Software.
Introduction SWE 619. Why Is Building Good Software Hard? Large software systems enormously complex  Millions of “moving parts” People expect software.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
Procedure specifications CSE 331. Outline Satisfying a specification; substitutability Stronger and weaker specifications - Comparing by hand - Comparing.
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
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.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Class 37: Computability in Theory and Practice cs1120 Fall 2011 David Evans 21 November 2011 cs1120 Fall 2011 David Evans 21 November 2011.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
1 Data Structures CSCI 132, Spring 2014 Lecture 3 Programming Principles and Life Read Ch. 1.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
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/
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
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,
Cs2220: Engineering Software Class 6: Defensive Programming Fall 2010 University of Virginia David Evans.
© Paul Ammann, 2008 Design by Contract Paul Ammann CS/SWE 332.
Cs205: engineering software university of virginia fall 2006 David Evans Substitution Principle.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, Mitchell, R., and McKim, Design by Contract,
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
CSE 311 Foundations of Computing I Lecture 28 Computability: Other Undecidable Problems Autumn 2011 CSE 3111.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 9: Designing Exceptionally.
1 Introduction 1. Why Data Structures? 2. What AreData Structure? 3. Phases of Software Development 4. Precondition and Postcondition 5. Examples.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
Cs2220: Engineering Software Class 13: Behavioral Subtyping Fall 2010 University of Virginia David Evans.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 7: A Tale of Two Graphs (and.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
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.
Modular Decomposition, Abstraction and Specifications
Input Space Partition Testing CS 4501 / 6501 Software Testing
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
Design by Contract Fall 2016 Version.
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.
Lecture 3: Abstraction by Specification CS201j: Engineering Software?
Data Abstraction David Evans cs205: engineering software
Lecture 7: A Tale of Two Graphs CS201j: Engineering Software
Lecture 4: Data Abstraction CS201j: Engineering Software
Algorithm and Ambiguity
CSE 143 Lecture 4 More ArrayIntList:
Go to pollev.com/cse143.
CSE 143 Lecture 5 More ArrayIntList:
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Lecture 13: Subtyping Rules Killer Bear Climber
Class 16: Concurrency Rules
CMSC 202 Exceptions.
Software Specifications
Presentation transcript:

cs2220: Engineering Software Class 4: Specifying Procedures Fall 2010 University of Virginia David Evans

Menu Specifications Return PS1

Managing Complexity Divide problem into subproblems that Can be solved independently Can be combined to solve the original problem How do we know they can be solved independently? How do we know they can be combined to solve the original problem?

Abstraction A I1I1 I2I2 I3I3 I4I4 An abstraction is a many-to-one map. I5I5

Using Abstractions A I1I1 I2I2 I3I3 I4I4 I5I5 Client When a client uses an abstraction, it should work as the client expects it to no matter which implementation is provided. How should client know what to expect?

Specification Tells the client of an abstraction what the client can expect it to do Tells the implementer of an abstraction what the implementation must do to satisfy the client Contract between client and implementer: – Client will only rely on behavior described by specification – Implementer will provide an implementation that satisfies the specification

Formality of Specifications Informal: written in a natural language (e.g., English) – People can disagree on what it means – Degrees of informality Formal: written in a specification language – Meaning is defined by specification language (whose meaning is defined precisely, but eventually informally) – May be analyzed by machines Note: people (e.g., Wes Weimer) also attempt to develop programs to analyze informal specifications, but its much harder! Specifications in cs2220

Good Specifications Clear, precise and unambiguous – Clients and implementers will agree on what they mean Complete – Describe the behavior of the abstraction in all situations Declarative – Describe what the abstraction should do, not how it should do it All specifications in cs2220 should strive for all of these Is it even possible for an informal specification to achieve all these?

What do you call people who decide what informal specifications mean?

Example Informal Specification Excessive bail shall not be required, nor excessive fines imposed, nor cruel and unusual punishments inflicted. 8 th Amendment

Correct Implementation? public static boolean violatesEigthAmendment (Punishment p) { // EFFECTS: Returns true if p violates the 8 th // amendment (cruel and unusual // punishments). return (p.isCruel () && p.isUnusual ()); } Or did they mean p.isCruel () || p.isUnusual () ?

Procedural Specifications Specification for a procedure describes: What its inputs are including their types and meanings The mapping between inputs and outputs What it can do to the state of the world

Parts of a Procedure Specification Header: name of procedure, types of parameters and return value – Java declaration (this is formal) Clauses: (comments in English) REQUIRES precondition the client must satisfy before calling EFFECTS postcondition the implementation satisfy at return

Specifications are Contracts Client promise: satisfy the precondition in REQUIRES clause Implementer promise: if client satisfies the precondition, when the function returns, the return value and state will satisfy the postcondition.

Specification Contract f () REQUIRES: precondition EFFECTS: postcondition precondition { f (); } postcondition If the precondition is true, after we call f() the postcondition is true.

Specification Example public String bestStock () // REQUIRES: false // EFFECTS: Returns the name of the // best stock to buy on the NASDAQ // tomorrow. Can we implement a procedure that satisfies this specification? Yes, any implementation will satisfy this specification! If the precondition in the requires clause is not satisfied, the procedure can do anything and still satisfy its specification!

Specification Example public String bestStock () // REQUIRES: true // EFFECTS: Returns the name of the // best stock to buy on the NASDAQ // tomorrow. Can we implement a procedure that satisfies this specification?

Preconditions The weaker (more easy to make true) the requires clause: – The more useful a procedure is for clients – The more difficult it is to implement correctly Avoid preconditions unless there is a really good reason to have one – Default requires clause is: REQUIRES true – Client doesn’t need to satisfy anything before calling

Specification Example public static int biggest (int [ ] a) // REQUIRES: true // EFFECTS: Returns the value of the // biggest element of a. Is this a good specification? Clear, precise and unambiguous Complete Declarative Clear, precise and unambiguous Complete Declarative

Specification Example public static int biggest (int [ ] a) // REQUIRES: a has at least one element. // EFFECTS: Returns the value of the // biggest element of a. Maybe, depends on the client. Its risky… Is this a good specification?

Bad Use of Preconditions Bug discovered in Microsoft Outlook that treats messages that start with “begin ” as empty attachments (can be exploited by viruses) To workaround this problem: Do not start messages with the word "begin" followed by two spaces. Use only one space between the word "begin" and the following data. Capitalize the word "begin" so that it is reads "Begin." Use a different word such as "start" or "commence ". To workaround this problem: Do not start messages with the word "begin" followed by two spaces. Use only one space between the word "begin" and the following data. Capitalize the word "begin" so that it is reads "Begin." Use a different word such as "start" or "commence ".

Specification Example public static int biggest (int [ ] a) // REQUIRES: true // EFFECTS: If a has at least one // element, returns the value biggest // element of a. Otherwise, returns // Integer.MIN_VALUE (smallest int value). Better, but client has to deal with special case now. Best would probably be to use an exception…

Specification Example public static int biggest (int [ ] a) thows NoElementException // REQUIRES: true // EFFECTS: Scans through each element in a, checking // if the value is bigger than the biggest previous value. // Returns the value of the biggest element. If the array // is empty, throws NoElementException. Is this a good specification? Clear, precise and unambiguous Complete Declarative Clear, precise and unambiguous Complete Declarative

Modifies How does a client know a is the same after biggest returns? public static int biggest (int [ ] a) thows NoElementException // REQUIRES: true // EFFECTS: If a has at least one element, // returns the value biggest element of a. // Otherwise, throws NoElementException. Reading the effects clause should be enough – if biggest modifies anything, it should describe it. But, that’s a lot of work.

Modifies MODIFIES clause: any state not listed in the modifies clause may not be changed by the procedure. public static int biggest (int [ ] a) // REQUIRES: true // MODIFIES: nothing // EFFECTS: If a has at least one element, // returns the value biggest element of a. // Otherwise, returns Integer.MIN_VALUE // (smallest int value).

Modifies Example public static int replaceBiggest (int [ ] a, int [] b) // REQUIRES: a and b both have at least one // element // MODIFIES: a // EFFECTS: Replaces the value of the biggest // element in a with the value of the biggest // element in b.

Defaults What should it mean when there is no REQUIRES? What should it mean when there is no MODIFIES? What should it mean when there is no EFFECTS? REQUIRES: true MODIFIES: nothing Meaningless.

Returning PS1 / PS2 Partners Blanton, James jb7bq; Smith, David dcs9z; Wallace, Alexander aww8rj Borja, Joseph jjb4wa; Noh, Brian bkn3yh Brown, Jeremy jpb4s; Marion, John jjm6p Chen, Jiamin jc2kk; Kalish, Michael mk8af Dewey-Vogt, Michael mkd5m; Sparkman, Elisabeth egs5u Dilorenzo, Jonathan jd9hz; Hearn, Charles cmh3eg Dollhopf, Niklaus nmd3ey; Oh, Uyn uho6r Featherston, Joseph jef5ez; Sun, Yixin ys3kz Hearn, Charles cmh3eg; Dilorenzo, Jonathan jd9hz Herder, Samuel srh5ne; Lopez, Erik ejl3tf Kalish, Michael mk8af; Chen, Jiamin jc2kk Lopez, Erik ejl3tf; Herder, Samuel srh5ne Marion, John jjm6p; Brown, Jeremy jpb4s Noh, Brian bkn3yh; Borja, Joseph jjb4wa Oh, Uyn uho6r; Dollhopf, Niklaus nmd3ey Smith, David dcs9z; Blanton, James jb7bq; Wallace, Alexander aww8rj Sparkman, Elisabeth egs5u; Dewey-Vogt, Michael mkd5m Sun, Yixin ys3kz; Featherston, Joseph jef5ez Wallace, Alexander aww8rj; Smith, David dcs9z; Blanton, James jb7bq

Which is better? /** * EFFECTS: Plays the old song forever. */ public static void playOldSong() { Player player new Player(); player.play(tune); playOldSong(); } /** * EFFECTS: Plays the old song forever. */ public static void playOldSong() { Player player new Player(); player.play(tune); playOldSong(); } /** * EFFECTS: Plays the old song forever. */ public static void playOldSong() { Player player = new Player(); while (true) { player.play(tune); } /** * EFFECTS: Plays the old song forever. */ public static void playOldSong() { Player player = new Player(); while (true) { player.play(tune); } Where’s the base case?

Which is better? public static void accelerateSong(String tune, int repeats, int tempo, double rate) { double currenttempo = tempo; for (int i = 0; i < repeats; i++) { playOldSongTempo(Integer.round(currenttempo)); currenttempo = currenttempo * rate; } public static void accelerateSong(String tune, int repeats, int tempo, double rate) { double currenttempo = tempo; for (int i = 0; i < repeats; i++) { playOldSongTempo(Integer.round(currenttempo)); currenttempo = currenttempo * rate; } public static void accelerateSong(String tune, int repeats, int tempo, double rate) { double currenttempo = tempo; for (int i = 0; i < repeats; i++) { playOldSongTempo(Integer.round(currenttempo)); currenttempo = currenttempo * rate; } public static void accelerateSong(String tune, int repeats, int tempo, double rate) { double currenttempo = tempo; for (int i = 0; i < repeats; i++) { playOldSongTempo(Integer.round(currenttempo)); currenttempo = currenttempo * rate; } Unlike Python, the Java compiler doesn’t care how you indent your code. But, you should! In Eclipse: use ctrl-I to indent your code structurally.

Finding Java Documentation Bing/Google: java se 1.6 ArrayList There are many old specs you will find first without this; some things have changed, so be careful to use the current specs. cannot be resolved to a variable Java compiler error messages: Java "cannot be resolved to a variable"

Charge Find your PS2 partner now – If you haven’t already finished part I, finish it soon so you can get started together on part II PS2 question 6: a lot for you to figure out on your own (but help will be available, and I’ll provide hints when you ask) – Keep things simple – Design for testability: check your code as you go