Docsity

Information and Software Engineering - Lecture Slides | SWE 619, Study notes of Engineering

Partial preview of the text

Download Information and Software Engineering - Lecture Slides | SWE 619 and more Study notes Engineering in PDF only on Docsity!

Information and Software Engineering

SWE 619 Fall 06

OO Software Spec. & Construction

http://ise.gmu.edu/~skaushik/courses/619.html

Abstraction SWE 619 Fall 06 (c) Saket Kaushik 2 Agenda „ Motivation

„ Examples of abstractions

„ Understand procedural abstractions

„ Benefits

„ Specify procedural abstractions

SWE 619 Fall 06 (c) Saket Kaushik 3 Software construction

„ Usually written in teams, ownership of code

(for maintainence,etc.) changes.

„ Client-Server software architecture

„ Contractual obligations

„ Need a way to express what a piece of code

does or what is it designed for.

„ Within the code block (supplementary doc.

not sufficient)

SWE 619 Fall 06 (c) Saket Kaushik 4 Software construction „ Need a methodology for

„ ‘High Quality’ software

„ reliability

„ minimizing bugs

„ robustness

„ Code should be

„ extendable,

„ flexible

SWE 619 Fall 06 (c) Saket Kaushik 7 Example 1: Merge Sort „ PROB: Sort an array in inc/dec order „ Example: sort [4,2,7,5,9,8] in inc. order

„ Several ways to do so: Bubble sort, bucket

sort, merge sort,etc.

„ Bubble sort – monolithic; finds successive

minimums.

„ Merge sort uses divide and rule principle;

reduces execution time

SWE 619 Fall 06 (c) Saket Kaushik 8 Example 1: Merge Sort Algo

„ Original problem – sort [4,2,7,5,9,8]

„ New subproblem – sort [4,2,7]; sort

[5,9,8]; combine them together

„ New subproblem – sort [4,2]; sort [7];

combine them

= combine [2,4] and [7] Æ [2,4,7] „ Similarly, combine [5,9] and [8] Æ [5,8,9] „ Finally, combine [2,4,7] and [5,8,9] Æ [2,4,5,7,8,9]

SWE 619 Fall 06 (c) Saket Kaushik 9 Example 2: Play „ Problem: Create a play by assembling a group of writers. „ Decomposition: Each writer is given a list of characters, general plot and is required to write dialogues for one of the characters. „ Individual solutions may not combine properly! SWE 619 Fall 06 (c) Saket Kaushik 10 Abstraction „ Improper decomposition is harmful! „ Abstraction changes level of detail to be considered

„ Helps to decompose a problem effectively

„ Abstracts from “irrelevant” details

„ Focus on the essence of the problem

„ Hence – simplifies analysis

SWE 619 Fall 06 (c) Saket Kaushik 13 Abstraction in programs „ Fundamental principal in program development

„ Makes programs much more manageable

and easier to understand

„ Reduces the detail to be considered.

„ Each abstraction is an independent entity

in itself

„ Procedures: breaking a program into a set

of small tasks

SWE 619 Fall 06 (c) Saket Kaushik 14 Example 3: Task Abstraction

public int locate(int [] accts, int k)

{ // forward search

for(int i=0; i<a.length; i++)

if (accts[i]==k)

return i; // at position i

return -1; // not in array

//Calling program int iArray={1,2,5,6,8}; int var = (3*8)/4; int index = locate(iArray, var); // abstract from a task

Consider a software for a bank that allows its members to log in,

check their account balance, deposit and withdraw money.

  • Four top level tasks can be further broken down. Consider one:

SWE 619 Fall 06 (c) Saket Kaushik 15

Example 3 (cont.)

public int locate(int [] accts, int k)

{ // backward search

for(int i=a.length; i>=0; i--)

if (accts[i]==k)

return i; // at position i

return -1; // not in array

} // Search Method

//Calling program

int iArray={1,2,5,6,8};

int var = (3*8)/4;

int index = locate(iArray, var);

//abstract from a task

/*calling code thus combines the

available functionality as

required.*/

We can change the implementation without affecting calling code!!

SWE 619 Fall 06 (c) Saket Kaushik 16 Abstraction

Implementation 1 … Implementation N

Abstraction: one to many

mapping

Specification

SWE 619 Fall 06 (c) Saket Kaushik 19 Benefits of abstraction

„ Locality

„ Each implementation can be understood in isolation „ An abstraction can be used without having to understand how it is implemented; it can be implemented without having to understand how it is used.

„ Modifiability

„ One implementation can be substituted for another without disturbing the using programs SWE 619 Fall 06 (c) Saket Kaushik 20 Kinds of Abstraction „ Procedural Abstraction: Lecture 2

„ In short: procedures

„ How to design, specify, write procedures

„ Properties of procedures

„ Data Abstraction: Lecture 4, „ Iteration Abstraction: Lecture 6 „ Type Hierarchy: Lecture 7 „ Polymorphic abstraction: Lecture 8

SWE 619 Fall 06 (c) Saket Kaushik 21 Abstraction Mechanisms

„ Abstraction by Parameterization

„ Abstracts identity of the data by replacing them with parameters „ Generalizes modules

„ Abstraction by Specification

„ Abstracts from the implementation details to the behavior users can depend on. „ Isolates modules from one another’s implementations. „ Language independence?? SWE 619 Fall 06 (c) Saket Kaushik 22 Abstraction by Parameterization „ Generalizes modules

„ How? -Separate definition from invocation

„ Example: Write a program to add two integers –: 1441 and 984

Approach 1: int sum = 1441 + 984;

Approach 2: int sum = add(1441, 984);

public int add(int x, int y){return x+y;}

„ Multiple invocations with same definition

Definition

Invocation

SWE 619 Fall 06 (c) Saket Kaushik 25 Abstraction by Specification „ Associate a specification with each procedure „ Meaning of procedure call is understood from this specification;not the body of the procedure „ No need to look at implementation to realize what the procedure is designed to accomplish SWE 619 Fall 06 (c) Saket Kaushik 26 Parts of a Procedural Specification

public static int sortedSearch (int[]a, int x)

// Requires: a is sorted in ascending order

// Effects: if x is in a returns an index where

// x is stored; otherwise, returns -

Precondition Header

Postcondition

SWE 619 Fall 06 (c) Saket Kaushik 27 Example (Liskov fig. 1.3) float sqrt (float coef)

// REQUIRES: coef > 0.

// EFFECTS: Returns an approximation to the square root of

coef

float ans = coef/2.0;

int i = 1;

while(i<7) {

ans = ans – ((ansans – coef)/(2.0ans));

i = i+1;

return ans;

}

Specification

One of the

Implementations

{ SWE 619 Fall 06 (c) Saket Kaushik 28 Procedures: an Abstraction

„ What are procedures?

„ May accept inputs; usually produce output „ Side effects? „ For same inputs, same output? „ Output for all possible inputs?

„ Procedures ~ Tasks.

„ By writing a procedure, you abstract from a task

„ Behavior of a procedure (what it does), is

described by a specification Abstraction interested in

the behavior only!

SWE 619 Fall 06 (c) Saket Kaushik 31 Yet Another Example

public static void sort (int[] a)

// Modifies: a

// Effects: rearranges the elements of a into

// ascending order

// E.g. if a = [3,1,6,1] before the call, then

// a = [1,1,3,6] after the call

Still no precondition (what if a is null?)

Notice implicit treatment of duplicates

Notice the “Modifies” clause

Alternate notation:

// E.g. if a = [3,1,6,1], then a_post = [1,1,3,6]

SWE 619 Fall 06 (c) Saket Kaushik 32 Yet Another Example

public static void copyLine()

// Requires: System.in contains a line of text

// Modifies: System.in and System.out

// Effects: Reads a line of text from System.in,

// advances the cursor in System.in to the end of

// the line, and writes the line on System.out

Procedures may modify variables that are not

explicit arguments

SWE 619 Fall 06 (c) Saket Kaushik 33 Requires-Modifies-Effects „ Must be sufficiently informative „ Requires states conditions that must be true at the beginning of execution „ Effects describes the conditions that will hold after execution provided conditions stated in Requires were satisfied. „ Modifies: modifications to input parameters „ If Requires conditions were satisfied (by the caller), Effects conditions are guranteed (by the callee). „ Software contracts SWE 619 Fall 06 (c) Saket Kaushik 34 What does a Contract Look Like?

„ Consider the triple: {P} S {Q}

„ P is the precondition (Requires clause) „ Q is the postcondition (Effects clause) „ S is the program text

„ The Customer (client) is obligated to establish

P.

„ The Implementor (service) may assume P

„ The Customer is entitled to Q

„ The Implementor is obligated to provide Q

„ That’s it!

SWE 619 Fall 06 (c) Saket Kaushik 37 Defensive Programming (cont) „ Meyer: “Don’t do it” „ You should look at the context (who calls the procedure)

„ “general” use: safety very important; it

might not be possible to check all inputs

„ “local” use or limited context: easy to

establish constraints on input

SWE 619 Fall 06 (c) Saket Kaushik 38 Designing Procedural Abstractions „ Simplicity „ Minimality

„ minimal means fewer constraints on behavior (what does

this say about the post condition?)

„ Underdetermined behavior

„ More than one possible result per input allowed

„ Deterministic implementation

„ Only one result per input produced

„ Generality

„ More general if specification can handle a large class of

inputs. (What does this say about the precondition?)

SWE 619 Fall 06 (c) Saket Kaushik 39 Total vs. Partial Procedures „ A procedure is total if its behavior is specified for all possible inputs. „ A partial procedure always has a precondition. „ Partial procedures are less safe than total procedures.

„ Exception handling can be used to eliminate

preconditions (more next week…).