good methods

42
Slide 1 Good Methods

Upload: addison-riddle

Post on 02-Jan-2016

23 views

Category:

Documents


2 download

DESCRIPTION

Good Methods. Cohesion and Coupling. For structured design These software metrics were used extensively Proven to be effective For object-oriented design Not clearly understood or effectively used No historic proof of effectiveness. Cohesion. - PowerPoint PPT Presentation

TRANSCRIPT

Slide 1

Good Methods

Slide 2

Cohesion and Coupling

l For structured design• These software metrics were used

extensively• Proven to be effective

l For object-oriented design• Not clearly understood or effectively used• No historic proof of effectiveness

Slide 3

Cohesion

l Measure of degree of interaction within a module

l Measure of the strength of association of the elements inside a module

l Functionality inside a module should be so related that anyone can easily see what the module does

l Goal is a highly cohesive module

Slide 4

Levels of Cohesion in Structured Design

l Functional cohesion (Good)l Sequential cohesionl Communicational cohesionl Procedural cohesionl Temporal cohesionl Logical cohesionl Coincidental cohesion (Bad)

Slide 5

Comparison of Cohesion Levelsfor Structured Design

Cohesion Level Cleanliness of Implementation

Reusability Modifiability Understand-ability

Functional Good Good Good Good

Sequential Good Medium Good Good

Communicational Good Poor Medium Medium

Procedural Medium Poor Variable Variable

Temporal Medium Bad Medium Medium

Logical Bad Bad Bad Poor

Coincidental Poor Bad Bad Bad

Slide 6

Cohesion

l For structured design• Deals with the cohesion of the actions in

a module (unit) to perform one and only one task

l For object-oriented methods• Deals with the ability of a module to

produce only one output for one module

Slide 7

Levels of Cohesion for Object-oriented Methods

l Functional cohesion (Good)l Sequential cohesionl Communicational cohesionl Iterative cohesionl Conditional cohesionl Coincidental cohesion (Bad)

Slide 8

Functional Cohesion for Object-oriented Methods

l Only one output exists for the modulel Ideal for object-oriented paradigm

Slide 9

Functional Cohesion for Object-oriented Methods

public void deposit (double amount)

{

balance = balance + amount;

}

Slide 10

Sequential Cohesion for Object-oriented Methods

l One output is dependent on the other output

l Modifications result in changing only one instance variable

Slide 11

Sequential Cohesion for Object-oriented Methods

public double withdraw (double amount, double fee){ amount = amount + fee;

if (amount < 0)System.out.println (“Error: withdraw amount is

invalid.”);else if (amount > balance)

System.out.println (“Error: Insufficient funds.”);else

balance = balance – amount;return balance;

}

Slide 12

Communicational Cohesion for Object-oriented Methods

l Two outputs are dependent on a common input

Slide 13

Communicational Cohesion for Object-oriented Methods

public void addCD (String title, String artist, double cost, int tracks)

{if (count = = collection.length)

increaseSize ( );collection [count] = new CD (title, artist, cost, tracks);totalCosts = totalCosts + cost;count++;

}

Slide 14

Iterative Cohesion for Object-oriented Methods

l Two outputs are iteratively dependent on the same input

Slide 15

Iterative Cohesion for Object-oriented Methods

void formDet (float Equations[2][3], float x[2][2], float y[2][2], float D[2][2])

{for (int Row = 0; Row < 2; ++Row)

for (int Col = 0; Col < 2; ++Col){

x[Row][Col] = Equations[Row][Col];y[Row][Col] = Equations[Row][Col];D[Row][Col] = Equations[Row][Col];

}x[0][0] = Equations[0][2];x[1][0] = Equations[1][2];y[0][1] = Equations[0][2];

y[1][1] = Equations[1][2];}

Slide 16

Conditional Cohesion for Object-oriented Methods

l Two outputs are conditionally dependent on the same input

Slide 17

Conditional Cohesion for Object-oriented Methods

public boolean checkBookIn ( ){

if (this.isAvailable ( )){ //this object cannot be checked out

System.out.println (“Error: “ + callNumber + “ is not checked out”);

return false;}else{

dueDate = null;availability = true;return true;

}}

Slide 18

Coincidental Cohesion for Object-oriented Methods

l Two outputs have no dependence relationship with each other and no dependence relation on a common input

Slide 19

Coincidental Cohesion for Object-oriented Methods

public void readInput ( ){

System.out.println (“Enter name of item being purchased: “);name = MyInput.readLine ( );System.out.println (“Enter price of item: “);price = MyInput.readLineDouble ( );System.out.println (“Enter number of items purchased: “);numberBought = MyInput.readLineInt ( );

}

Slide 20

Coincidental Cohesion for Object-oriented Methods

public String AcceptItemName ( ){

System.out.println (“Enter name of item being purchased: “);name = MyInput.readLine ( );return name;

}

Slide 21

Cohesion Decision Tree for Object-oriented Methods

Functional Cohesion yes

Does the module modify fewer than 2 object variables Sequential Cohesion yes no Do all modifications actually result in the change to only one variable Communicational Cohesion yes no Are the output(s) dependent on common input but not derived in a loop or a

select statement Iterative Cohesion yes

no Are the output(s) dependent on common input and are they used in a loop Conditional Cohesion yes no Are the output(s) dependent on common input and are they used in a selection no Coincidental Cohesion

Slide 22

Coupling

l Measure of degree of interaction between two modules

l Measure of interdependence of modulesl Goal is to have so little coupling that

changes can be made within one module without disrupting other modules

Slide 23

Types of Coupling for Structured Methods

l Data (Good)l Stampl Controll Commonl Content (Bad)

Slide 24

Comparison of Coupling Typesfor Structured Design

Coupling Type Susceptibility to Ripple Effect

Modifiability Understand-ability

Module’s Usability in Other Systems

Data Variable Good Good Good

Stamp Variable Medium Medium Medium

Control Medium Poor Poor Poor

Common Poor Medium Bad Bad

Content Bad Bad Bad Bad

Slide 25

Data Coupling for Structured Methods

l Just the data needed is passed between the calling and called modules through calling parameters

Slide 26

Data Coupling for Structured Methods

Compute Bill

Calculate Interest

InterestBill Amount

Slide 27

Stamp Coupling for Structured Methods

l Whole data structure being passed as a parameter to a module but the module needs only part of data in the data structure

l Types of stamp coupling:• natural

• table

• record

• bundle

Slide 28

Control Coupling for Structured Methods

l One module passes a flag as parameter to control the logic in the other module

l Parameter being passed is a control flag which dictates what action to perform within the module

l Example:void ProcessTransactions (bool whichFlag,

DataRec data, bool& successFlag);

Slide 29

Common Coupling for Structured Methods

l Use of global datal Two modules have access to the same

datal Why not?

• Hard to maintain (ripple effect)• Decreases reusability• Hard to debug

• Data change• Module change

Slide 30

Content Coupling for Structured Methods

l One module reaches into the internal code of another module to grab or deposit data or to control its function

Slide 31

Coupling Decision Tree for Structured Methods

No Coupling yes

Is there no data shared between modules?

Data Coupling no Is simple data or a data structure

passed from one module yes to another with all the structure elements used? Stamp Coupling Are only portions of the data no structure being passed actually yes used by the module?

no Control Coupling Does one module pass a flag yes

to control the logic of the other module?

Common Coupling no Does one module read or write yes to the global data?

no Common Coupling Does one module reference yes The contents of another module?

Slide 32

Coupling Example

p

r s

q

t u

1

2

3 4

5 6

# in out

1 Aircraft type Status flag

2 --- List of aircraft parts

3 Function code

---

4 --- List of aircraft parts

5 Part number Part manufacturer

6 Part number Part name

p, t, and u access the same database in update mode

Slide 33

Coupling Example

p q r s t u

p Data Data or Stamp

Common Common

q Control Data or stamp

r Data

s Data

t Common

u

Slide 34

Levels of Coupling for Object-oriented Methods

l No coupling (Good)l Sequential couplingl Computational couplingl Conditional couplingl Common couplingl Content coupling (Bad)

Slide 35

No Coupling for Object-oriented Methods

l No global data sharing or functional calls between two modules

Slide 36

Sequential Coupling for Object-oriented Methods

l Two modules exist where the outputs of one module are the inputs of another

Slide 37

Computational Coupling for Object-oriented Methods

l Two modules exist where one module passes a parameter to another module and the parameter has control or data dependence on the output

Slide 38

Conditional Coupling for Object-oriented Methods

l One module passes a parameter to another module and the parameter has control dependence on an output

Slide 39

Common Coupling for Object-oriented Methods

l One module writes to the global data and another module reads from the global data

Slide 40

Content Coupling for Object-oriented Methods

l One module references the contents of another module

Slide 41

Coupling Decision Tree for Object-oriented Methods

Slide 42

Coupling Decision Tree for Object-oriented Methods

From this point forward, Design Reviews will be done using these metrics.