7/31/2019 CouplingandCohesion CSE VI SEM
1/40
Coupling and Cohesion
Prof. ASHISH MISHRA
7/31/2019 CouplingandCohesion CSE VI SEM
2/40
Characteristics of Good Design
Component independence
High cohesion
Low coupling
Exception identification and handling
Fault prevention and fault tolerance
7/31/2019 CouplingandCohesion CSE VI SEM
3/40
Coupling: Degree of dependence
among components
No dependencies Loosely coupled-some dependencies
Highly coupled-many 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.
7/31/2019 CouplingandCohesion CSE VI SEM
4/40
Range of Coupling
High Coupling
Loose
Low
Content
Common
Control
Stamp
Data
Uncoupled
7/31/2019 CouplingandCohesion CSE VI SEM
5/40
Content coupling
Definition: One component references
contents of another
Example:
Component directly modifies anothers data
Component refers to local data of another
component in terms of numerical
displacement
Component modifies anothers code, e.g.,
jumps into the middle of a routine
7/31/2019 CouplingandCohesion CSE VI SEM
6/40
Example of Content Coupling-1
Part of program handles lookup for customer.
When customer not found, component adds
customer by directly modifying the contents ofthe data structure containing customer data.
7/31/2019 CouplingandCohesion CSE VI SEM
7/40
Example of Content Coupling-2
Part of program handles lookup for customer.
When customer not found, component addscustomer by directly modifying the contents of
the data structure containing customer data.
Improvement:
When customer not found, component calls the
AddCustomer() method that is responsible formaintaining customer data.
7/31/2019 CouplingandCohesion CSE VI SEM
8/40
Common Coupling
Definition: Two components share data
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 affecta data element (reduces maintainability)
Difficult to reuse components
Reduces ability to control data accesses
7/31/2019 CouplingandCohesion CSE VI SEM
9/40
Example-1
Each source process writes directly to global data
store. Each sink process reads directly from global
data store.
Process control component maintains current data
about state of operation. Gets data from multiple
sources. Supplies data to multiple sinks.
7/31/2019 CouplingandCohesion CSE VI SEM
10/40
Example-2
Each source process writes directly to globaldata store. Each sink process reads directlyfrom global data store.
ImprovementData manager component is responsible fordata in data store. Processes send data to andrequest data from data manager.
Process control component maintains current data
about state of operation. Gets data from multiple
sources. Supplies data to multiple sinks.
7/31/2019 CouplingandCohesion CSE VI SEM
11/40
Control Coupling
Definition: Component passes control
parameters to coupled components.
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 reuseof functionality
7/31/2019 CouplingandCohesion CSE VI SEM
12/40
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.
7/31/2019 CouplingandCohesion CSE VI SEM
13/40
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 tomanipulate 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.
7/31/2019 CouplingandCohesion CSE VI SEM
14/40
Example-1
The print routine of the customer billing
accepts a customer data structure as anargument, parses it, and prints the name,
address, and billing information.
Customer billing system
7/31/2019 CouplingandCohesion CSE VI SEM
15/40
Example-2
The print routine of the customer billing
accepts a customer data structure as anargument, parses it, and prints the name,address, and billing information.
ImprovementThe print routine takes the customer name,address, and billing information as anargument.
Customer Billing System
7/31/2019 CouplingandCohesion CSE VI SEM
16/40
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.
7/31/2019 CouplingandCohesion CSE VI SEM
17/40
Key Idea in Object-Oriented
Programming Object-oriented designs tend to have low
coupling.
7/31/2019 CouplingandCohesion CSE VI SEM
18/40
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. Internal glue with which component is
constructed
All elements of component are directed toward
and essential for performing the same task
High is good
7/31/2019 CouplingandCohesion CSE VI SEM
19/40
Range of Cohesion
High Cohesion
Low
FunctionalInformational
Sequential
Communicational
Procedural
Temporal
Logical
Coincidental
7/31/2019 CouplingandCohesion CSE VI SEM
20/40
Coincidental Cohesion
Definition: Parts of the component are only
related by their location in source code
Elements needed to achieve some
functionality are scattered throughout the
system.
Accidental
Worst form
7/31/2019 CouplingandCohesion CSE VI SEM
21/40
Example
Print next line
Reverse string of characters in second
argument
Add 7 to 5th argument
Convert 4th argument to float
7/31/2019 CouplingandCohesion CSE VI SEM
22/40
Logical Cohesion
Definition: Elements of component are
related logically and not functionally.
Several logically related elements are in
the same component and one of the
elements is selected by the client
component.
7/31/2019 CouplingandCohesion CSE VI SEM
23/40
Example-1
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 aresignificantly different.
7/31/2019 CouplingandCohesion CSE VI SEM
24/40
Example-2
A component reads inputs from tape, disk,and network. All the code for thesefunctions are in the same component.Operations are related, but the functionsare significantly different.
Improvement
A device component has a read operation
that is overridden by sub-classcomponents. The tape sub-class readsfrom tape. The disk sub-class reads fromdisk. The network sub-class reads from thenetwork.
7/31/2019 CouplingandCohesion CSE VI SEM
25/40
Temporal Cohesion
Definition: Elements of a component are
related by timing.
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.
7/31/2019 CouplingandCohesion CSE VI SEM
26/40
Example-1
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.
7/31/2019 CouplingandCohesion CSE VI SEM
27/40
Example-2
A system initialization routine: this routinecontains all of the code for initializing all ofthe parts of the system. Lots of different
activities occur, all at init time.
Improvement
A system initialization routine sends an
initialization message to each component. Each component initializes itself at
component instantiation time.
7/31/2019 CouplingandCohesion CSE VI SEM
28/40
Procedural Cohesion
Definition: Elements of a component are
related only to ensure a particular order of
execution.
Actions are still weakly connected and
unlikely to be reusable
7/31/2019 CouplingandCohesion CSE VI SEM
29/40
Example
...
Read part number from
data base
update repair record onmaintenance file.
...
May be useful toabstract the intent ofthis sequence. Makethe data base and
repair recordcomponents handlereading and updating.Make component that
handles moreabstract operation.
7/31/2019 CouplingandCohesion CSE VI SEM
30/40
Communicational Cohesion
Definition: Module performs a series of
actions related by a sequence of steps to
be followed by the product and all actions
are performed on the same data
7/31/2019 CouplingandCohesion CSE VI SEM
31/40
Example
Update record in data
base and send it to
the printer.
database.Update
(record).
record.Print().
7/31/2019 CouplingandCohesion CSE VI SEM
32/40
Sequential Cohesion
The output of one component is the input to
another.
Occurs naturally in functional programming
languages
Good situation
7/31/2019 CouplingandCohesion CSE VI SEM
33/40
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 ADT and object-oriented paradigm promote
7/31/2019 CouplingandCohesion CSE VI SEM
34/40
Functional Cohesion
Definition: Every essential element to a
single computation is contained in the
component.
Every element in the component is
essential to the computation.
Ideal situation.
7/31/2019 CouplingandCohesion CSE VI SEM
35/40
Examples of Cohesion-1
Function A
Function
BFunction
D
Function
CFunction
E
Coincidental
Parts unrelated
Function A
Function A
Function A
logic
Logical
Similar functions
Time t0
Time t0 + X
Time t0 + 2X
Temporal
Related by time
Function A
Function B
Function C
Procedural
Related by order of functions
7/31/2019 CouplingandCohesion CSE VI SEM
36/40
Examples of Cohesion-2
Function A part 1
Function A part 2
Function A part 3
Functional
Sequential with complete, related functions
Function A
Function B
Function C
Communicational
Access same data
Function A
Function B
Function C
Sequential
Output of one is input to another
7/31/2019 CouplingandCohesion CSE VI SEM
37/40
p
q
t
r s
u
12
3 4
5 6
1 Aircraft type Status flag
2 ----- List of aircraft
parts3 Function code -----
4 ----- List of aircraft
parts5 Part number Part manufacturer
6 Part number Part name
No. In Out
Interface Description
p, t, u access the same
database in update
mode
Problem: Define coupling between pairs of modules.
7/31/2019 CouplingandCohesion CSE VI SEM
38/40
q r s t u
p
q --- ---
r --- ---
s ---
t
Coupling between pairs of modules
7/31/2019 CouplingandCohesion CSE VI SEM
39/40
Problem: Classify cohesion for
each module Compute average daily temperatures at various
sites
Initialize sums and open files
Create new temperature record Store temperature record
Close files and print average temperatures
Read in site, time, and temperature
Store record for specific site
Edit site, time, or temperature field
7/31/2019 CouplingandCohesion CSE VI SEM
40/40
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.