Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE 310: Software Engineering

Similar presentations


Presentation on theme: "EECE 310: Software Engineering"— Presentation transcript:

1 EECE 310: Software Engineering
Modular Decomposition, Abstraction and Specifications

2 Lecture Outline Modular Decomposition Abstraction Kinds Specifications
Procedural abstractions

3 Program Decomposition
Goal: Reducing complexity Developing Maintaining Modifying Understanding Using Based on recognition of useful abstractions

4 Decomposition Goal: Create small programs that interact with one another in simple, well-defined ways Criteria for good decomposition Sub-problems at the same level of detail Each can be solved independently Can be combined to solve the original problem

5 Decomposition Example
Large Unsolved Problem SP2 SP3 SP4 SP5 SP6 SP1 Combined solution

6 What often happens in practice ?
Large Unsolved Problem SP1 SP2 SP3 SP7 SP4 SP5 SP9 Combined solution ?

7 Abstraction A way to do decomposition productively Definitions:
Application of many-to-one mapping Simplification of the problem that hides irrelevant details, characteristics Grouping of related entities into distinct categories without worrying about the details

8 Lecture Outline Modular Decomposition Abstraction Kinds Specifications
Procedural abstractions

9 Abstraction Kinds By Parameterization By Specification
Abstracts from the identity of the data By Specification Abstracts from implementation details to the behavior users can depend on Isolates modules from one another’s implementations Provides a clear contract between the user and the implementer of module

10 Abstraction by Parameterization
Write the procedure once, but change its parameters Generalizes modules so they can be used many times Fundamental technique used in programming Example: Recipe for making pasta (Serves 2 people) Step 1: Boil 2 cups of water in a sauce-pan Step 2: Add 6 cups of pasta when water comes to a boil Step 3: Sautee 5 grams of vegetables in an another pan Step 4: Mix the sauteed vegetables with the cooked pasta Step 5: Add pasta sauce, cheese and salt to taste But, what if we wanted to make pasta for 5 people ? Should we develop a new recipe from scratch ?

11 Abstraction by Specification
Hide implementation details of a module and focus instead on what it does (rather than how it does it) Specify the behavior that module’s users can depend on Can generalize across multiple implementations Change module’s behavior without changing its usage Example: Recipe for making pasta (Serves 2) Step 1: Boil 2 cups of water in a sauce-pan Step 2: Add 6 cups of pasta when water comes to a boil Step 3: Sautee 5 grams of vegetables in an another pan Step 4: Mix sauteed vegetables with the cooked pasta Step 5: Add pasta sauce, cheese and salt to taste

12 Benefits of Abstraction by Specification
Locality -- the implementation of an abstraction can be understood (or written) without needing to examine the implementations of any other abstractions. 2. Modifiability -- an abstraction can be re-implemented without requiring changes to any abstractions that use it.

13 Lecture Outline Modular Decomposition Abstraction Kinds Specifications
Procedural abstractions

14 Specification Contract between client and implementer
Client will rely only on behavior in specification Implementer only needs to provide this behavior Implementer is free to change implementation Tells the client of an abstraction what the procedure is expected to do Tells the implementer of an abstraction what implementation must do to satisfy the client

15 Specifications: Formal Vs Informal
Formal Specs Informal Specs Written in a mathematically precise language Have only one meaning or a finite set of meanings Can be understood by humans and machines Automated tools can check code against specifications Written in an informal language (English) May have different meanings depending on the reader (or the programmer) Can be understood only by humans (for the most part) Manual effort needed to check code against specs For now we will write informal specifications, but will attempt to make them as precise as possible

16 Recipes: Analogy A good specification is like a recipe and has the following components: What is being done ? What is required ? How to do it ? Are there things that one needs to look for ? Any other things to note Recipes are written in a standard format defined by the Chefs association

17 Recipe: History and Trivia
Earliest recipe -> stone tablet in Babylonia (1600 BC). Recipes popular in Roman & Greek times Gained popularity with invention of printing press Early recipes often imprecise -> served more as a reminder to someone who knew how to prepare the dish e.g., “Do it till it is done” Modern recipe format invented around 1816 by Dr. William Kitchener: “The cook’s oracle” Used precise terms such as “add 1 quart of vinegar” Listed ingredients and name of dish at beginning Specified processes such as boil, steam, fry in detail Recipe Book: circa 1507

18 Recommendations for recipe writing
The following is what the chef’s association recommends for recipe writers today Use a straightforward descriptive name for the dish, rather than a name that is fun or creative Indicate preparation and cooking times at the beginning List ingredients separately at the top of the recipe. List the main ingredient (such as meat) first

19 How to write procedural specifications ?
Choose a name for the procedure that is descriptive and (preferably) pronounceable Procedural specifications should consist of: REQUIRES: What does the procedure need ? MODIFIES: What data does the procedure modify ? EFFECTS: What does the procedure produce at the end ? The code for the procedure is not part of its specification, but must match the specification

20 Procedural Specifications - 1
Factorial: Fact(n) = 1 * 2 *3 …. * n * (n- 1) static int Fact(int n) { // EFFECTS: Returns the factorial of n // where fact(n) = 1 * 2 * … * n // REQUIRES: n >= 0 // MODIFIES: None (this can be dropped) }

21 Procedural Specifications – 2 (Multiple Implementations)
Using Iteration Using recursion public static int fact(int N ) { int result = 1; for (int i =1; i<=N; i++) { result = result * i; } return result; public static int fact(int N) { int result; if (N==0) result = 1; else result = N * fact(N – 1); return result; } Both implementations (should) have the same behavior. Can you see any difference ?

22

23 Group Activity Write the specification for a sorting routine. You should not constrain the sorting to a specific algorithm or implementation. However, you should take care that the sorting does what you would “expect”. Trade the specification with another group and have them write an implementation for it

24 Sorting Routine: Informal Spec
Take an array as argument, sort it in place What kind of arrays do we want to handle ? Arrays of ints (primitive type) Array should be non-null (or maybe not) No need to say that array elements are non-null (implied by first clause)

25 REQUIRES clause Should only capture the requirements on array that are not implied by the type signature Do not need to say anything about the types of the arguments, unless they cannot be inferred Do not mention the return type of the procedure public static void sort( int[] a ) { // REQUIRES: Array a not be Null }

26 MODIFIES Clause Modifies clause only specifies what objects (on heap) are modified by the procedure Does not include return value Does not say anything about how it is modified public static void sort( int[] a ) { // REQUIRES: Array a not be Null // MODIFIES: Array a

27 EFFECTS Clause Specifies what the EFFECT of the procedure is
Include both return value and heap objects Include any exceptions thrown by the procedure Must say how the modifications are made public static void sort( int[] a ) { // REQUIRES: Array a not be Null // MODIFIES: Array a // EFFECTS: Rearranges the elements of a in // ascending order (i.e., lowest to highest)

28 Implementation: Bubble sort
public static void sort( int[] a ) { // REQUIRES: Array a not be Null // MODIFIES: Array a // EFFECTS: Array a is sorted in ascending order int i, j; for (i= 0; i<a.length; i++) for (j=i+1; j<a.length; j++) if (a[i] > a[j]) { // Swap a[i] and a[j] }

29 Group Activity Write a specification for the sort program that sorts an array of String objects (in alphabetical order) public static void sort( String[] a ) { // REQUIRES: // MODIFIES: // EFFECTS:

30 Solution: Group Activity
The array may contain null objects Need to put this in the REQUIRES clause public static void sort( String[] a ) { // REQUIRES: a is not NULL && Elements of a are not NULL // MODIFIES: Array a // EFFECTS: Re-arranges the elements of array a // in alphabetical order (i.e., lowest to highest) // Uses String.compareTo for determining order

31 Procedural Specifications: Example 1
public class Vectors { //OVERVIEW: Provides useful standalone procedures // for manipulating vectors public static void removeDuplicates ( Vector v ) { // REQUIRES: No element of vector v is null // MODIFIES: v // EFFECTS: Removes all duplicate elements from v; // uses equals method to determine duplicates. }

32 Procedural Specifications: Example 2
if (v== null) return; // v may be NULL as per REQUIRES for (int i = 0; i < v.size(); ++i) { Object x = v.get(i); int j = i + 1; while ( j < v.size() ) { if ( ! x.equals( v.get(j) ) ) j++; else { v.set(j, v.lastElement() ); v.remove( j ); } Does the above implementation satisfy the specification ? Why or why not ?

33 Procedural Specification: Example 3
Does the following implementation satisfy the specification ? public static void removeDuplicates ( Vector v ) // REQUIRES: No element of vector v is null // MODIFIES: v // EFFECTS: Removes all duplicate elements from v; // uses equals method to determine duplicates. // The order of the remaining elements may change v.clear(); }

34 Procedural Specifications: Example 4
public class Vectors { //OVERVIEW: Provides useful standalone procedures // for manipulating vectors public static void removeDuplicates ( Vector v ) { // REQUIRES: No element of vector v is null // MODIFIES: v // EFFECTS: Removes all duplicate elements from v; // uses the equals method to determine duplicates. // The order of the remaining elements may change // but the set of elements formed by the new vector is // identical to the set of elements formed by the old one }

35 What’s the problem ? The original specification was too weak
Any implementation could satisfy it, even incorrect ones The specification should be sufficiently restrictive to allow only correct implementations If it is important to you, then write it down From the point of view of the client Make sure you can use the specification

36 Remember … Specifications are many-to-one mappings
Many implementations may satisfy a specification But some may be wrong because the specification is not restrictive enough ! A specification is like a contract. If the implementer can get away by doing less and yet satisfy the specification, they may choose to ! It is your responsibility to put it down in writing !

37 What makes a good specification ?
Sufficiently restrictive Minimally constraining Clear – no room for ambiguity These guidelines are necessarily vague, so let us see some examples

38 Sufficiently Restrictive
public static int search (int[] a, int x) // REQUIRES: a is not null // EFFECTS: Examines each of a[0], a[1], ... , in order and returns the index of the element that equals to x. What’s wrong with the above specification ? What if there is more than one element equal to x ? What if there is no element equal to x ? How will you fix these issues ?

39 Minimally Constraining
Do you really care that the procedure examines each element in turn ? Also, do you need the first element that equals x ? Precludes non-linear implementations such as binary search public static int search (int[] a, int x) // REQUIRES: a is not null // EFFECTS: Examines each of a[0], a[1], ... , in some order and // returns the index of an element that equals to x. // If no such element is found, it returns - 1

40 Note the use of i.e. to indicate redundancy
Clarity The above specification is NOT clear. Do we return an element whose index is x, or do we return the index of an element that equals x. This is the problem with natural language. So we need to be more clear. public static int search (int[] a, int x) // REQUIRES: a is not null // EFFECTS: Examines each of a[0], a[1], ... , in some order and // returns the index of an element that equals to x. // i.e., returns an i, such that a[i] = x and 0 <=i <a.length // if no such element is found, it returns -1. Note the use of i.e. to indicate redundancy

41 Group Activity Specify minimum procedure in Java that returns the index of the element in an array of integers that has minimum value. For example, if it’s given an array {1, 2, -1}, it would return the index ‘2’. Aim at writing specification that is sufficiently restrictive, general, and clear. Use the procedure in another one to find the kth smallest element in an array. Write the spec and the implementation.

42 Summary Abstraction: Needed for doing modular decomposition productively By parameterization: Standard for programming By specification: Focus of this class Provides locality and modifiability Specifies contract between implementer & user Allows implementation to vary independent of client Good specifications are sufficiently restrictive, general and as clear as possible Specifications should ensure that they do not permit incorrect implementations

43 To do before next class Read Chapters 1, 3 and 9 in the textbook
Try exercises 3.1 to 3.4 and 3.7 Attempt Assignment 1 on class webpage Need to choose a partner for the assignments Learn Java NOW if you haven’t started yet ! If you did badly in Quiz 1, rethink taking the class

44 Under-determined Behavior Vs. Non-Deterministic Implementation
Procedure specification can specify underdetermined behaviour Procedure implementation can have non-deterministic implementation

45 Under-determined behaviour
A procedure is underdetermined if for certain inputs its specification allows more than one possible result. For example, public static Integer findMax(Integer x, Integer y, Integer z) // REQUIRES: x, y, and z are not null // EFFECTS: returns the largest number out of the 3 input integers, // e.g., if x = 10, y = 3, z = -7, then return x public static int search (int[] a, int x) throws NotFoundException, NullPointerException // EFFECTS: If a is null throws NullPointerException, else // returns i such that a[ i ] = x; // If no such element is found, it returns -1

46 A deterministic implementation
An implementation of a procedure is deterministic if, for the same inputs, it always produces the same result. Most procedures will be in this category Examples of procedures that require nondeterministic implementations int random (int min, int max) // REQUIRES: max > min + 1 // EFFECTS: returns a random integer result such that min < result < max


Download ppt "EECE 310: Software Engineering"

Similar presentations


Ads by Google