Modularization, Cohesion and Coupling
Modularization, Cohesion and Coupling
Further Modularization
Bimo Sunarfri Hantono
Electrical Engineering and Information Technology
Gadjah Mada University
Further modularization
Objectives
To further develop modularization using a more complex problem
To introduce cohesion as a measure of the internal strength of a
module
To introduce coupling as a measure of the extent of information
interchange between modules
Outline
Steps in modularization
Module cohesion
Module coupling
Steps in modularization
1. Define the problem by dividing it into its three components:
input, output and processing.
2. Group the activities into subtasks or functions to determine
the modules that will make up the program.
3. Construct a hierarchy chart to illustrate the modules and their
relationship to each other.
4. Establish the logic of the mainline of the algorithm in
pseudocode.
5. Develop the pseudocode for each successive module in the
hierarchy chart.
6. Desk check the solution algorithm.
Module Cohesion
Cohesion is a measure of the internal strength of a module,
how closely the elements or statements of a module are
associated with each other.
Edward Yourdon and Larry Constantine
Cohesion level
Coincidental
Logical
Temporal
Procedural
Communicational
Sequential
Functional
Cohesion attribute
Low cohesion
High cohesion
Strongest
Coincidental Cohesion
Elements are collected into a module simply because they
happen to fall together
There is no meaningful relationship between the elements at
all, and so it is difficult to concisely define the function of the
module
Conditions that cause it happens:
Because of hardware constrictions,
To conform to a badly considered programming standard,
To reduce the number of modules in a program or to increase the
number of statements in a module to a particular minimum number
File_processing
Open employee updates file
Read employee record
Print_page_headings
Open employee master file
Set page_count to one
Set error_flag to false
END
Logical Cohesion
occurs when the elements of a module are grouped together
according to a certain class of activity
the elements fall into some general category because they all
do the same kind of thing
An example might be a module that performs all the read
statements for three different files: a sort of 'Read_all_files'
module
Read_all_files (file_code)
CASE of file_code
1 : Read customer transaction record
IF not EOF
increment customer_transaction_count
ENDIF
2 : Read customer master record
IF not EOF
increment customer_master_count
ENDIF
3 : Read product master record
IF not EOF
increment product_master_count
ENDIF
ENDCASE
END
Temporal Cohesion
occurs when the elements of a module are grouped together
because they are related by time
Typical examples are, initialization and finalization modules
Initialization
Open transaction file
Issue prompt Enter todays date - DDMMYY
Read todays_date
Set transaction_count to zero
Read transaction record
IF not EOF
increment transaction_count
ENDIF
Open report file
Print_page_headings
Set report_total to zero
END
Procedural cohesion
occurs when the elements of a module are related because
they operate according to a particular procedure
typical example : mainline of a program
Read_student_records_and_total_student_ages
Set number_of_records to zero
Set total_age to zero
Read student record
DOWHILE more records exist
Add age to total_age
Add 1 to number_of_records
Read student record
ENDDO
Print number_of_records, total_age
END
Communicational cohesion
A module with communication cohesion has elements that are
related by a reference to the same input or output data.
An example of this could be a module to "print and puch
record
Validate_product_record
IF transaction_type NOT = 0 THEN
error_flag = true
error_message = invalid transaction type
Print_error_report
ENDIF
IF customer_number is NOT numeric THEN
error_flag = true
error_message = invalid customer number
Print_error_report
ENDIF
IF product_no = blanks OR product_no has leading blanks THEN
error_flag = true
error_message = invalid product no
Print_error_report
ENDIF
END
Sequential cohesion
occurs when a module contains elements that depend on the
processing of previous elements
Process_purchases
Set total_purchases to zero
Get number_of_purchases
DO loop_index = 1 to number_of_purchases
get purchase
add purchase to total_purchases
ENDDO
sales_tax = total_purchases * sales_tax_percent
amount_due = total_purchases + sales_tax
END
Functional cohesion
occurs when all the elements of a module contribute to the
performance of a single specific task
The module can be easily named by a single verb followed by
a two-word object
Calculate_sales_tax
IF product is sales tax exempt THEN
sales_tax = 0
ELSE
IF product_price < $50.00 THEN
sales_tax = product_price * 0.25
ELSE
IF product_price < $100.00 THEN
sales_tax = product_price * 0.35
ELSE
sales_tax = product_price * 0.5
ENDIF
ENDIF
ENDIF
END
Module Coupling
Coupling is a measure of the extent of information interchange
between modules.
Tight coupling implies large dependence on the structure of
one module by another
Modules with loose coupling are more independent and easier
to maintain.
Coupling level
Common
External
Control
Stamp
Data
Tight coupling
Resultant module
design quality
Poorest
Loose coupling
Best
Coupling attribute
Common Coupling
occurs when modules reference the same global data
structure
Module A
Module B
A Read_customer_record
Read customer record
IF EOF THEN
set EOF_flag to true
ENDIF
END
B Validate_customer_record
IF customer_number is NOT numeric THEN
error_message = invalid customer number
Print_error_report
ENDIF
:
:
END
External Coupling
occurs when two or more modules access the same global
data variable
Module A
Module B
A Calculate_sales_tax
IF product is sales exempt THEN
sales_tax = 0
ELSE
IF product_price < $50.00 THEN
sales_tax = product_price * 0.25
:
:
ENDIF
ENDIF
END
B Calculate_amount_due
:
:
amount_due = total_amount + sales_tax
END
Control Coupling
occurs when a module passes another module a control
variable that is intended to control the other module's logic
These control variables are referred to as program flags, or
switches, and are passed between modules in the form of
parameters
Module A
flag
Module B
A Process_input_code
Read input_code
Choose_appropriate_action (input_code)
:
:
END
B Choose_appropriate_action (input_code)
CASE OF input_code
1 : Read employee record
2 : Print_page_headings
3 : Open employee master file
4 : Set page_count to zero
5 : error_message = Employee number not numeric
ENDCASE
END
Stamp Coupling
occurs when one module passes a non-global data structure
to another module in the form of a parameter
Module A
data structure
Module B
A Process_transaction_record
:
:
IF transaction record is for a male THEN
Process_male_student (current_record)
ELSE
Process_female_student (current_record)
ENDIF
:
:
END
B Process_male_student (current_record)
increment male_student_count
IF student_age > 21 THEN
increment mature_male_count
ENDIF
END
Data Coupling
occurs when a module passes a non-global data variable to
another module
Module A
elementary data
item
Module B
Process_customer_record
:
:
Calculate_sales_tax (total_price, sales_tax)
:
END
Chapter Summary
Cohesion and coupling were introduced and must be
considered when designing modular programs.
Cohesion is a measure of the internal strength of a module.
The higher the cohesion, the better the module. Seven levels
of cohesion were given and each level was discussed, with a
pseudocode example provided.
Coupling is a measure of the extent of information interchange
between modules. The fewer the connections between the
modules, the more loosely they are coupled, offering good
module design quality. Five levels of coupling were given and
each level was discussed, with a pseudocode example
provided.
$27.00
Private
Business
Private
Business
Private
Business
5% of wholesale price
7% of wholesale price
1% of weight (converted to $)
3% of weight (converted to $)
1% of wholesale price
2% of wholesale price
Processing
Get input details
Calculate federal_tax
Calculate registration_costs
Calculate
total_amount_payable
Output details to screen
Output
vehicle_make
vehicle_model
body_type
registration_fee
tax_levy
weight_tax
insurance_premium
total_registration_charges
federal_tax
total_amount_payable
vehicle_details
Get_vehicle_
details
Calculate_total_
amount-payable
Calculate_
federal_tax
Output_registration_
details
Calculate_
registration_costs
1
2
3
4
5
6
Calculate_ vehicle_registration
Read owners_name
DOWHILE owners_name NOT = XXX
Get_vehicle_details (vehicle_details)
Calculate_total_amount_payable (vehicle_details, registration_costs)
Output_registration_details (vehicle_details, registration_costs)
Read owners_name
ENDDO
END
Get_vehicle_details (vehicle_details)
7
Prompt and Get vehicle_make
8
Prompt and Get vehicle_model
9
Prompt and Get weight
10
Prompt and Get body_type
11 Prompt and Get usage_code
12 Prompt and Get wholesale_price
END
Calculate_ total_amount_payable (vehicle_details , registration_costs)
13 Calculate_federal_tax (vehicle_details, federal_tax)
14 Calculate_registration_costs (vehicle_details, registration_costs)
15 total_amount_payable = federal_tax + total_registration_charges
END
16
17
18
19
20
Calculate_registration_ costs (vehicle_details, registration_costs)
21 registration_fee = $27.00
22 IF usage_code = P THEN
tax_levy = wholesale_price * 0.05
weight_tax = weight * 0.01
insurance_premium = wholesale_price * 0.01
ELSE
tax_levy = wholesale_price * 0.07
weight_tax = weight * 0.03
insurance_premium = wholesale_price * 0.02
ENDIF
23 total_registration_charges = registration_fee + tax_levy + weight_tax +
insurance_premium
END
24
25
26
27
28
29
30
31
32
33
Thank You