0% found this document useful (0 votes)
21 views26 pages

Lecture 9 Cohesion and coupling

Uploaded by

vishp6270
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views26 pages

Lecture 9 Cohesion and coupling

Uploaded by

vishp6270
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

“Impart high quality knowledge & skills to students in the field of information technology, motivate research,

encourage industry consultancy projects & nurture human values and life skills”.

Cohesion and Coupling

Session by

Dr. Aisha Fernandes


Associate Professor,Information
Technology Department
Goa Engineering College.

[email protected]
Organization of this Lecture:

n Understanding Characteristics of a Good Design


n What is Coupling?
n Types of Coupling
n Cohesion
n Types of Cohesion
n Cohesion v/s Coupling
n Summary
n References

Lecture 9 Cohesion n
Coupling 2
Learning Outcome

To be able to explain Cohesion and Coupling with


examples

Lecture 9 Cohesion n
Coupling 3
Characteristics of Good Design

n Modularization
(process of dividing a software system into multiple independent modules where
each module works independently)
Advantages of Modularization:
n Easy to understand the system.
n System maintenance is easy.
n A module can be used many times as their requirements. No need to
write it again and again.

n Component independence
n High cohesion M1 M2
n Low coupling

M3
Lecture 9 Cohesion n
Coupling
4
Coupling

n is the measure of the degree of interdependence


between the modules.
A good design is the one that has low coupling

No dependencies Loosely coupled Highly coupled


some dependencies many dependencies

Lecture 9 Cohesion n
Coupling
5
https://www.javatpoint.com/software-engineering-coupling-and-cohesion
Lecture 9 Cohesion n
Coupling 6
Data Coupling

-- Component passes data (not data structures)


to another component.
If the dependency between the modules is based on the fact that they
communicate by passing only data, then the modules are said to be
data coupled. In data coupling, the components are independent to
each other and communicating through data.

n Good, if it can be achieved.

n Example: Customer billing system


The print routine takes the customer name, address, and
billing information as arguments.

Lecture 9 Cohesion n
Coupling
7 7
Stamp Coupling

-- Component passes a data structure to another


component that does not have access to the entire
structure.

n In stamp coupling, the complete data structure is passed from one


module to another module. May be necessary due to efficiency
factors: this is a choice made by insightful designer, not lazy
programmer.

n Example:
Passing structure variable in C or object in C++ language to a
module.

Lecture 9 Cohesion n
Coupling
8 8
Control Coupling

-- Component passes control parameters to coupled


components.
Control Coupling exists among two modules if data from one module is used to
direct the structure of instruction execution in another.

n May be either good or bad, depending on situation.


-Bad if parameters indicate completely different behavior
-Good if parameters allow factoring and reuse of functionality

n Good example: Sort that takes a comparison function as an


argument.
The sort function is clearly defined: return a list in sorted order,
where sorted is determined by a parameter.

9 9
External Coupling

-- Two components share something externally


imposed, e.g., External file, protocol, etc.

n External Coupling arises when two modules share an


externally imposed data format, communication protocols,
or device interface. This is related to communication to
external tools and devices.

10
Common Coupling

-- More than one component share data such as


global data structures
Two modules are common coupled if they share information
through some global data items. The changes in global data
mean tracing back to all modules which access that data to
evaluate the effect of the change.
n Usually a poor design choice because
n Reduces readability
n Reduces maintainability
n Difficult to reuse components
n Reduces ability to control data accesses
Example :
n 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.

11 11
Content Coupling

-- One component modifies another.

In content coupling, one module can modify the data of another


module or control flow is passed from one module to the other
module.
This is the worst form of coupling and should be avoided.
.
n Example:
Component modifies another’s code, e.g., jumps (goto)
into the middle of a routine

12 12
Cohesion

§Cohesion is a measure that defines the degree of intra-


dependability within elements of a module.
OR
The degree to which all elements of a component are directed
towards a single task.

§The greater the cohesion, the better is the program design.

Lecture 9 Cohesion n
Coupling 13
https://www.javatpoint.com/software-engineering-coupling-and-cohesion
Lecture 9 Cohesion n
Coupling 14
Functional Cohesion

-- Functional Cohesion is said to exist if the different


elements of a module, cooperate to achieve a single
well defined function.

n Every element in the component is essential to the computation.


n It is the most Ideal situation
n Example :
Function A part 1

Function A part 2

Function A part 3
Sequential with complete, related functions

15 15
Sequential Cohesion

-- The output of one part is the input to another.

n When elements of module are grouped because the output of one


element serves as input to another and so on, it is called sequential
cohesion.Occurs naturally in functional programming languages
n Good situation
n Example :
Function A

Function B

Function C

Output of one is input to another

16 16
Communicational Cohesion
-- Functions performed on the same data or to
produce the same data.
When elements of module are grouped together, which are executed
sequentially and work on same data (information), it is called
communicational cohesion. Two elements operate on the same input data or
contribute towards the same output data.
n Examples:
n Update record in database and send it to the printer
n Fetch unrelated data at the same time(To minimize disk access)

Function A

Function B

Function C

17 17
Procedural Cohesion
--When elements of module are grouped together, which are
executed sequentially in order to perform a task, it is
called procedural cohesion.
Elements of procedural cohesion ensure the order of execution.
Actions are still weakly connected and unlikely to be reusable.

n Example:
1. Calculate student GPA,
n print student record, Function A
n calculate cumulative GPA,
n print cumulative GPA.... Function B
2. Write output record
n Read new input record Function C
n Pad input with spaces
n Return new record
Related by order of functions

18
Temporal Cohesion
n Elements are related by timing involved
n Elements are grouped by when they are processed.

Example:
n 1. An exception handler that Closes all open files,Creates an error log
Notifies user,Lots of different activities occur, all at same time.
n 2. 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.

Time t0
Time t0 + X
Time t0 + 2X

Related by time
19 19
Logical Cohesion

n Elements of component are related logically and not


functionally.
n A module is said to be logically cohesive if all the elements
of the module perform a similar operation.
Examples:
1. Error handling, data input and data output, etc.
2. 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.

Function A

Function A’

Function A’’
Similar functions
20 20
Coincidental Cohesion
-- A module is said to have coincidental cohesion if it
performs a set of tasks that are associated with
each other very loosely, if at all.
It is unplanned and random cohesion.
n Accidental
n Worst form
Example : print next line and reverse the characters of a string in a
single component.
Function A
Function Function
B C
Function Function
D E
Parts unrelated
21 21
Coupling v/s Cohesion
Coupling Cohesion

Coupling is also called Inter-Module Binding. Cohesion is also called Intra-Module Binding.

Coupling shows the relationships between Cohesion shows the relationship within the
modules. module.

Coupling shows the Cohesion shows the module's


relative independence between the modules. relative functional strength.

While creating, you should aim for low While creating you should aim for high
coupling, i.e., dependency among modules cohesion, i.e., a cohesive component/ module
should be less. focuses on a single function (i.e., single-
mindedness) with little interaction with other
modules of the system.

In coupling, modules are linked to the other In cohesion, the module focuses on a single
modules. thing.

Lecture 9 Cohesion n
Coupling 22
Summary

Coupling is the measure of the degree of interdependence between the


modules.
Cohesion is a measure that defines the degree of intra-dependability
within elements of a module.

Lecture 9 Cohesion n
Coupling 23
References :

1. Stephen Schach ,“Object Oriented and


Classical Software Engineering”, Tata Mc
Graw Hill Publication.

Lecture 9 Cohesion n
Coupling 24
Any Questions…??

Lecture 9 Cohesion n
Coupling 25
Lecture 9 Cohesion n
Coupling 26

You might also like