Coupling and Cohesion

Download Report

Transcript Coupling and Cohesion

Coupling and Cohesion
Schach, S, R. Object-Oriented and Classical
Software Engineering. McGraw-Hill, 2002
Characteristics of Good Design
• Component independence
–
–
• Exception identification and handling
• Fault prevention and fault tolerance
• Design for change
M1
M2
M3
2
Cohesion
• Definition: The degree to which all elements of a
component are directed towards a single task
and all elements directed towards that task are
contained in a single component.
•
• All elements of component are directed toward
and essential for performing the same task
• High is good
3
Range of Cohesion
High Cohesion
Functional
Informational
Sequential
Communicational
Procedural
Temporal
Logical
Coincidental
Low
4
Coincidental Cohesion
• Definition:
• Elements needed to achieve some
functionality are scattered throughout the
system.
• Accidental
• Worst form
Functional
Informational
Sequential
Communicational
Procedural
Temporal
Logical
Coincidental
5
Example
• Print next line
• Reverse string of characters in second
argument
• Add 7 to 5th argument
• Convert 4th argument to float
6
Logical Cohesion
• Definition:
• Several logically related elements are in
the same component and one of the
elements is selected by the client
component.
Functional
Informational
Sequential
Communicational
Procedural
Temporal
Logical
Coincidental
7
Example
• A component reads inputs from tape, disk,
and network. All the code for these
functions are in the same component.
• Operations are related, but the functions are
significantly different.
8
Example
• A component reads inputs from tape, disk,
and network. All the code for these
functions are in the same component.
Operations are related, but the functions
are significantly different.
Improvement
9
Temporal Cohesion
• Definition:
• Difficult to change because you may have
to look at numerous components when a
change in a data structure is made.
• Increases chances of regression fault
• Component unlikely to
be reusable.
Functional
Informational
Sequential
Communicational
Procedural
Temporal
Logical
Coincidental
10
Example
• A system initialization routine: this routine
contains all of the code for initializing all of the
parts of the system. Lots of different activities
occur, all at init time.
11
Example
• A system initialization routine: this routine
contains all of the code for initializing all of
the parts of the system. Lots of different
activities occur, all at init time.
Improvement
•
•
12
Procedural Cohesion
• Definition:
• Actions are still weakly connected and
unlikely to be reusable
Functional
Informational
Sequential
Communicational
Procedural
Temporal
Logical
Coincidental
13
Example
...
Read part number from
data base
update repair record on
maintenance file.
...
•
14
Communicational Cohesion
• Definition: Module performs a series of
actions (a sequence of steps to be
followed by the product) and all actions
are performed on the same data
Functional
Informational
Sequential
Communicational
Procedural
Temporal
Logical
Coincidental
15
Example
Module determine customer details
use customer account no
find customer name
find customer loan balance
return customer name, customer loan balance
endmodule
16
Sequential Cohesion
• The output of one component is the input to
another.
• Occurs naturally in functional programming
languages
• Good situation
Functional
Informational
Sequential
Communicational
Procedural
Temporal
Logical
Coincidental
17
Informational Cohesion
• Definition: Module performs a number of
actions, each with its own entry point,
with independent code for each action,
all performed on the same data.
• Different from logical cohesion
– Each piece of code has single entry and
single exit
– In logical cohesion,
actions of module intertwined
Functional
Informational
Sequential
Communicational
Procedural
Temporal
Logical
Coincidental
18
Functional Cohesion
• Definition:
• Every element in the component is
essential to the computation.
• Ideal situation.
Functional
Informational
Sequential
Communicational
Procedural
Temporal
Logical
Coincidental
19
Examples of Cohesion-1
Function A
Function Function
B
C
Function Function
D
E
Coincidental
Parts unrelated
logic
Function A
Time t0
Function A’
Time t0 + X
Function A’’
Time t0 + 2X
Logical
Similar functions
Temporal
Related by time
Function A
Function B
Function C
Procedural
Related by order of functions
20
Examples of Cohesion-2
Function A
Function A
Function B
Function B
Function C
Function C
Communicational
Access same data
Sequential
Output of one is input to another
Function A part 1
Function A part 2
Function A part 3
Functional
Sequential with complete, related functions
21
Coupling: Degree of dependence
among components
No dependencies
Highly coupled-many dependencies
Loosely coupled-some dependencies
High coupling makes modifying
parts of the system difficult, e.g.,
modifying a component affects all
the components to which the
component is connected.
22
Range of Coupling
High Coupling
Content
Common
Control
Stamp
Loose
Data
Uncoupled
Low
23
Content coupling
• Definition:
• Example:
– Component directly modifies another’s data
– Component refers to local data of another component
in terms of numerical displacement
– Component modifies
– another’s code, e.g., jumps into the
High Coupling
Content
middle of a routine
Common
Control
Stamp
Loose
Data
24
Uncoupled
Low
Example of Content Coupling
Part of program handles lookup for customer.
When customer not found, component adds
customer by directly modifying the contents of
the data structure containing customer data.
25
Example of Content Coupling
Part of program handles lookup for customer.
When customer not found, component adds
customer by directly modifying the contents of
the data structure containing customer data.
Improvement:
26
Common Coupling
• Definition:
– Global data structures
– Common blocks
• Usually a poor design choice because
– Lack of clear responsibility for the data
– Reduces readability
– Difficult to determine all the components that affect
a data element (reduces maintainability)
– Difficult to reuse components
High Coupling
Content
– Reduces ability to control
Common
data accesses
Control
Stamp
Loose
Data
Uncoupled
27
Low
Example
Process control component maintains current data
about state of operation. Gets data from multiple
sources. Supplies data to multiple sinks.
Each source process writes directly to global data store.
Each sink process reads directly from global data store.
28
Example
Process control component maintains current data
about state of operation. Gets data from multiple
sources. Supplies data to multiple sinks.
Each source process writes directly to global data store.
Each sink process reads directly from global data store.
Improvement
29
Control Coupling
• Definition:
• May be either good or bad, depending on
situation.
– Bad when component must be aware of
internal structure and logic of another module
– Good if parameters allow factoring
and reuse of functionality
Content
High Coupling
Common
Control
Stamp
Loose
Data
30
Uncoupled
Low
Example
• Acceptable: Module p calls module q and q
passes back flag that says it cannot complete
the task, then q is passing data
• Not Acceptable: Module p calls module q and q
passes back flag that says it cannot complete
the task and, as a result, writes a specific
message.
31
Stamp Coupling
• Definition: Component passes a data structure
to another component that does not have access
to the entire structure.
• Requires second component to know how to
manipulate the data structure (e.g., needs to
know about implementation)
• May be necessary due to efficiency factors: this
is a choice made by insightful
designer, not lazy programmer.
Content
High Coupling
Common
Control
Stamp
Loose
Data
Uncoupled
32
Low
Example
Customer billing system
The print routine of the customer billing
accepts a customer data structure as an
argument, parses it, and prints the name,
address, and billing information.
33
Example
Customer Billing System
The print routine of the customer billing
accepts a customer data structure as an
argument, parses it, and prints the name,
address, and billing information.
Improvement
34
Data Coupling
• Definition: Two components are data
coupled if there are homogeneous data
items.
• Every argument is simple argument or
data structure in which all elements are
used
• Good, if it can be achieved.
• Easy to write contracts for
this and modify component
independently.
Content
High Coupling
Common
Control
Stamp
Loose
Data
Uncoupled
Low
35
Key Idea in Object-Oriented
Programming
• Object-oriented designs tend to have low
coupling.
36
Problem: Classify cohesion for
each module
•
•
•
•
Input control items, add the items, and verify totals.
Initialize sums and open files.
Print transaction and copy transaction to tape.
Open files, obtain first transaction and first master
record, and print page headings.
• Update record on file and get next transaction.
• Produce either a sales report, a project status
report, or a customer transaction report.
• Check syntactic correctness of vehicle guidance
parameters.
37
Problem: Define coupling between pairs of modules.
p
1
2
q
3
t
Out
Status flag
2
-----
3
Function code
List of aircraft
parts
-----
4
-----
List of aircraft
parts
5
Part number
Part manufacturer
Part number
Part name
4
r
5
No.
In
1
Aircraft type
s
6
u
p, t, u access the same
6
database in update
mode
Interface Description
38
Coupling between pairs of modules
q
r
s
t
u
---
---
---
p
q
----
r
----
----
---
s
----
----
----
---
t
----
----
----
----
---
39
In Class
• P1: What is the effect of cohesion on
maintenance?
• P2: What is the effect of coupling on
maintenance?
• P3: Produce an example of each type of
cohesion. Justify your answers.
• P4: Produce an example of each type of
coupling. Justify your answers.
• P5: Is there a tradeoff between cohesion and
coupling? Justify your answer.
40