University of Toronto Department of Computer Science CSC444 Lec06 1 Lecture 6: Procedural Abstractions Defining procedural abstractions the parts of a.

Slides:



Advertisements
Similar presentations
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
Advertisements

COMPSCI 105 S Principles of Computer Science 12 Abstract Data Type.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
API Design CPSC 315 – Programming Studio Fall 2008 Follows Kernighan and Pike, The Practice of Programming and Joshua Bloch’s Library-Centric Software.
Software Engineering and Design Principles Chapter 1.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Object (Data and Algorithm) Analysis Cmput Lecture 5 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this.
CS 201 Functions Debzani Deb.
Chapter 1 Principles of Programming and Software Engineering.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
University of Toronto Department of Computer Science © 2001, Steve Easterbrook CSC444 Lec12 1 Lecture 12: Software Design Quality What is software quality?
The Project AH Computing. Functional Requirements  What the product must do!  Examples attractive welcome screen all options available as clickable.
Introduction SWE 619. Why Is Building Good Software Hard? Large software systems enormously complex  Millions of “moving parts” People expect software.
University of Toronto Department of Computer Science CSC444 Lec04- 1 Lecture 4: Software Lifecycles The Software Process Waterfall model Rapid Prototyping.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
University of Toronto Department of Computer Science © 2001, Steve Easterbrook CSC444 Lec22 1 Lecture 22: Software Measurement Basics of software measurement.
Language Evaluation Criteria
Lecture 18: Specifications
5.1 and 5.4 through 5.6 Various Things. Terminology Identifiers: a name representing a variable, class name, method name, etc. Operand: a named memory.
Algorithms Describing what you know. Contents What are they and were do we find them? Why show the algorithm? What formalisms are used for presenting.
1 Shawlands Academy Higher Computing Software Development Unit.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
University of Toronto Department of Computer Science CSC444 Lec08 1 Lecture 8: Testing Verification and Validation testing vs. static analysis Testing.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
University of Toronto Department of Computer Science CSC444 Lec05- 1 Lecture 5: Decomposition and Abstraction Decomposition When to decompose Identifying.
1 The Software Development Process  Systems analysis  Systems design  Implementation  Testing  Documentation  Evaluation  Maintenance.
CS Fall 2007 Dr. Barbara Boucher Owens. CS 2 Text –Main, Michael. Data Structures & Other Objects in Java Third Edition Objectives –Master building.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
University of Toronto Department of Computer Science © 2001, Steve Easterbrook CSC444 Lec23 1 Lecture 23: Course Summary Course Goals Summary of what we.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
SE: CHAPTER 7 Writing The Program
1 CSC 222: Computer Programming II Spring 2004 See online syllabus at: Course goals:
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Software Engineering Principles. SE Principles Principles are statements describing desirable properties of the product and process.
Lecture 11 Data Structures, Algorithms & Complexity Introduction Dr Kevin Casey BSc, MSc, PhD GRIFFITH COLLEGE DUBLIN.
1 Ch. 1: Software Development (Read) 5 Phases of Software Life Cycle: Problem Analysis and Specification Design Implementation (Coding) Testing, Execution.
Data Abstaraction Chapter 10.
The Software Development Process
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
ECSE Software Engineering 1I HO 4 © HY 2012 Lecture 4 Formal Methods A Library System Specification (Continued) From Specification to Design.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Intermediate 2 Computing Unit 2 - Software Development.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
CSCI1600: Embedded and Real Time Software Lecture 28: Verification I Steven Reiss, Fall 2015.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005.
1 The Software Development Process ► Systems analysis ► Systems design ► Implementation ► Testing ► Documentation ► Evaluation ► Maintenance.
1 Introduction 1. Why Data Structures? 2. What AreData Structure? 3. Phases of Software Development 4. Precondition and Postcondition 5. Examples.
Efficiently Solving Computer Programming Problems Doncho Minkov Telerik Corporation Technical Trainer.
Chapter 2 Principles of Programming and Software Engineering.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
Chapter 7 Continued Arrays & Strings. Arrays of Structures Arrays can contain structures as well as simple data types. Let’s look at an example of this,
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
Advanced Algorithms Analysis and Design
CompSci 280 S Introduction to Software Development
CSC 222: Computer Programming II
Design by Contract Fall 2016 Version.
Algorithms Chapter 3 With Question/Answer Animations
Objective of This Course
Boolean Expressions to Make Comparisons
Presentation transcript:

University of Toronto Department of Computer Science CSC444 Lec06 1 Lecture 6: Procedural Abstractions Defining procedural abstractions the parts of a procedural abstraction total vs. partial procedures side effects Implementing procedural abstractions defensive programming optimization some comments on program style Note: procedural abstraction applies to any language, no matter what the units are called: procedures (e.g. Ada, Modula,…) functions (e.g. C, ML,…) methods (e.g. java,…) © 2001, Steve Easterbrook

University of Toronto Department of Computer Science CSC444 Lec06 2 A procedure maps from input to output parameters it may modify its parameters it may have side effects it may return a result aim for “Referential Transparency”  procedure does the same thing, no matter where it is used  basis of the Cleanroom approach A procedural abstraction (“specification”):  describes what a procedure does, ignores how it does it  different implementations of the abstraction can differ over details  one implementation can be substituted for another Advantages Locality: programmers don’t need to know implementation details Modifiability: replacing an implementation does not affect the rest of the system Language Independence: implementation could be any programming language Procedural Abstractions © 2001, Steve Easterbrook

University of Toronto Department of Computer Science CSC444 Lec06 3 can we ship this many in 42 days? how many do we have right now? how many are coming in? how many are leaving? how many in each warehouse? all warehouses how many are in transit? how many will be produced? how many already sold? how many will be lost to shrinkage? all trucks all customers all plants all warehouses problem decomposition procedural abstraction parameters: item id quantity needed ship date

University of Toronto Department of Computer Science CSC444 Lec06 4 Abstractions need to be precisely defined formally (mathematically): very precise; can be automatically checked informally (e.g. natural language description): less precise, easier to read and write Need to define five things: 1.The way in which the procedure communicates (input/output parameters) 2.The conditions under which the procedure will work 3.What the procedure achieves 4.Any side effects (changes to global variables or system state) 5.Any exceptions raised Defining abstractions procedure sort(a:array of int, len:int) returns array of int requires: a is an array that is at least len integers long effects: returns a copy of the array a with its elements sorted into ascending order modifies: reduces available heap space by n * sizeof(int) raises: arraybounderror if a is not a valid pointer to an array of length len; memerror if there is insufficent heap space for a new array of length len procedure sort(a:array of int, len:int) returns array of int requires: a is an array that is at least len integers long effects: returns a copy of the array a with its elements sorted into ascending order modifies: reduces available heap space by n * sizeof(int) raises: arraybounderror if a is not a valid pointer to an array of length len; memerror if there is insufficent heap space for a new array of length len © 2001, Steve Easterbrook

University of Toronto Department of Computer Science CSC444 Lec06 5 Total vs. Partial Procedures A total procedure works for any input (within the type checking restrictions of the language) hence has no requires clause e.g. A partial procedure works for some of the possible inputs e.g. (square root only works for non-negative integers) The requires clause places restrictions on the operation of the procedure The procedure is only guaranteed to work if the requires clause is met procedure length(a: stack of int) returns b:int effects: b is the number of elements in a procedure length(a: stack of int) returns b:int effects: b is the number of elements in a procedure sqrt (a:int) returns b: real requires: a  0 effects: b is an approximation of the square root of a to within ±10 -4 procedure sqrt (a:int) returns b: real requires: a  0 effects: b is an approximation of the square root of a to within ±10 -4 © 2001, Steve Easterbrook

University of Toronto Department of Computer Science CSC444 Lec06 6 Specifying Side Effects Side effects If a procedure modifies its environment in any way, this is a side effect e.g. modifying global variables e.g. allocating or de-allocating memory e.g. printing text on the screen (actually: writing to the output stream) e.g. reading characters from the keyboard (actually: consuming the input stream) A pure function has no side effects all communication is through its parameters and return result All programming languages allow procedures/functions to have side effects input/output is impossible otherwise(!) but side effects make a program harder to understand and more prone to error Use of ‘modifies’ procedure readlines (n:int) returns s:list of strings requires n>=0 modifies: advances the input stream by up to n lines effects: s is a list of up to n strings, containing characters on the next n lines of input. Newline characters are not included in the strings procedure readlines (n:int) returns s:list of strings requires n>=0 modifies: advances the input stream by up to n lines effects: s is a list of up to n strings, containing characters on the next n lines of input. Newline characters are not included in the strings procedure initialize_counter() returns old:int modifies: the global variable count is set to zero effects: old is set to the value of count before initialization procedure initialize_counter() returns old:int modifies: the global variable count is set to zero effects: old is set to the value of count before initialization © 2001, Steve Easterbrook

University of Toronto Department of Computer Science CSC444 Lec06 7 Different Implementations Many possible implementations: linear search - slow but easy to implement binary search - fast for large lists … These satisfy the abstraction, but: What if x occurs more than once? What if a is not sorted? If we care about any of these details, they should be described in the abstraction. procedure search (a: list of int, x:int) returns i:int requires: a is sorted in ascending order effects: If x is in a, i is the index of an occurrence of x in a, so that a[i]=x otherwise i is -1 procedure search (a: list of int, x:int) returns i:int requires: a is sorted in ascending order effects: If x is in a, i is the index of an occurrence of x in a, so that a[i]=x otherwise i is -1 © 2001, Steve Easterbrook

University of Toronto Department of Computer Science CSC444 Lec06 8 Procedure Design Procedural abstractions: …have users and an implementor the abstraction defines the service offered to the users the implementor is free to provide the service in whatever way seems best (As long as it meets the specification) The abstraction should: constrain things that matter to the user e.g. whether sort creates a new list or modifies the old one… not constrain things that don’t matter to the user e.g. speed, efficiency, algorithm used… Under-determination “some aspects of behavior are not defined” e.g. search was underdetermined as we didn’t say what to do if the element occurs more than once in the list. an under-determined specification may have implementations that behave differently © 2001, Steve Easterbrook

University of Toronto Department of Computer Science CSC444 Lec06 9 Minimally specified only constrained to extent required by the users General able to work on a range of inputs (or input types) e.g. search could be generalized to work on any array types...we might need to pass it a comparison function BUT: generalizing a procedure is only worthwhile if it becomes more useful c.f. moving a method up the class hierarchy Simple a well-defined and easily explained purpose tip: if you can’t think of a simple name for your procedure, it’s probably overly complex (= not cohesive) Non-trivial should achieve something significant don’t decompose a program down into too many tiny pieces Desirable properties of procedures © 2001, Steve Easterbrook

University of Toronto Department of Computer Science CSC444 Lec06 10 Defensive Programming Murphy’s law: anything that can go wrong will go wrong e.g. if you rely on precedence order for expressions, you’ll make a mistake, so put brackets everywhere x * y + a * b (x * y) + (a * b) e.g. people will call your procedure with the wrong inputs, will forget to initialise data, etc, so always check! Partial Procedures are Problematic sooner or later someone will violate the ‘requires’ clause either: try to make them total or: add code at the beginning that checks the requires clause is met © 2001, Steve Easterbrook

University of Toronto Department of Computer Science CSC444 Lec06 11 Further advantages of abstraction Encapsulation all the important information about the procedure is stated explicitly in one place the detail is hidden Testing without an abstraction defined, how will you know if your procedure is correct? the abstraction will suggest unusual (“off nominal”) test cases Optimization It is often hard to predict where bottlenecks will occur use abstractions to implement the whole program, then just optimize those procedures that need optimizing Error tracing abstractions help you build firewalls that stop errors propagating © 2001, Steve Easterbrook

University of Toronto Department of Computer Science CSC444 Lec06 12 Elements of Program Style Program code is an expression of a design that will change: write clearly, avoid cleverness use library functions avoid temporary variables clarity is more important than efficiency parenthesize to avoid ambiguity avoid confusing variable names don’t patch bad code - rewrite it don’t over-comment don’t comment bad code - rewrite it format the code for readability As a design, program code should convey intellectual clarity clarity is better than small gains in efficiency make it right before you make it faster make it robust before you make it faster make it clear before you make it faster choose a data representation that makes the program simple Program code represents the result of problem solving write first in a simple pseudo-code then refine modularize write and test a big program in small pieces instrument your programs measure for bottlenecks before you optimize watch for “off-by-one” errors test the “boundary conditions” checks some answers by hand Assumptions are dangerous test inputs for validity and plausibility identify bad input, recover if possible use self-identifying input make input easy to prepare make output self-explanatory make sure the code “does nothing” gracefully Source: Adapted from Blum, 1992, p278-9 © 2001, Steve Easterbrook

University of Toronto Department of Computer Science CSC444 Lec06 13 Summary Procedural abstractions are useful they express the contract between user and implementor they are helpful for testing they facilitate modification Procedural abstractions must be defined precisely “abstract” does not mean the same as “vague”! strive for referential transparency This process works at all levels The principles shown here for procedures apply to all design levels: specify the abstraction precisely the specification should tell you everything you need to know to use the component the specification should not include unnecessary design information Try it for: systems, CSCIs, modules, packages, procedures, loops,... © 2001, Steve Easterbrook

University of Toronto Department of Computer Science CSC444 Lec06 14 References van Vliet, H. “Software Engineering: Principles and Practice (2nd Edition)” Wiley,  deals with procedural abstraction briefly in section But you’ll also need to refer to: Liskov, B. and Guttag, J., “Program Development in Java: Abstraction, Specification and Object-Oriented Design”, 2000, Addison-Wesley.  Chapter 3. I draw on Liskov’s ideas extensively for advice on program design in this course. The commenting style I use (“requires”, “effects”, etc) is Liskov’s. If you plan to do any extensive programming in Java, you should buy this book. If you don’t buy it, borrow it and read the first few chapters. Blum, B. “Software Engineering: A Holistic View”. Oxford University Press, 1992  Blum does an nice treatment on program design and abstractions (see especially section 4.2) Prowell, S. J, Trammell, C. J, Linger, R., and Poore, J. H. “Cleanroom Software Engineering”, 1999, Addison-Wesley  The cleanroom approach relies heavily on encapsulation and referential transparency. It demonstrates how abstraction and specification can be used in the same way at each level of design. © 2001, Steve Easterbrook