Module cohesion in software engineering
The notion of cohesion was introduced with coupling in the mid-1970s, also as a way of characterizing good procedural program design.
Cohesion is the measure of strength of the association of elements within a module. In other words, the extent to which all instructions in a module relate to a single function is called Cohesion. In a truly cohesive module, all of the instructions in the module pertain to performing a single unified task. It is also referred as Intra-module Cohesion.
Cohesion is the property or characteristic of an individual module.
Cohesion in modules should be maximized. Maximally cohesive modules also tend to be the most loosely coupled, so achieving high levels of cohesion in the system design also helps minimize coupling.
If a module is designed to perform one and only one function then it has no need to know about the interior workings of other modules. The cohesive module only needs to take data it is passed, act on them, and pass its output on to its super-ordinate module.
A module cohesion is determined by such considerations.
- Cohesion is highest in modules that have a single, clear, logically independent responsibility.
- Cohesion degrades as unrelated responsibilities or tasks are added to module.
- One practical and long-standing practice for achieving cohesion is to form modules that implement data types.
Novice designers often have a tendency to assign responsibilities to software components arbitrarily, destroying module cohesion.
One of the great advantages of the object-oriented analysis and design paradigm is that it encourages designers to create components that mimic real-world entities. Real-world entities tends to have appropriate responsibilities, so modelling them in software generally produces cohesive modules.
types of module cohesion in software engineering
Various levels of cohesion were defined based on the functionality placed in a module. Low cohesion modules included functionality by accident or because of temporal coincidence or logical similarity, while high cohesion modules performed a single action or did a single operation on a data structure.
Here, seven types of cohesions.
- Functional Cohesion
- Sequential Cohesion
- Communicational Cohesion
- Procedural Cohesion
- Temporal Cohesion
- Logical Cohesion
- Coincidental Cohesion
Functional Cohesion
- The name of module will indicate its functions.
- All statement within a module are based on one function.
- It is the best cohesion as the module perform a single specific function.
example. We are taking example of Calculating_Sales_Tax function.
IF PRODUCT IS SALES_TAX EXEMPT THEN
SALES_TAX = 0
ELSE
IF PRODUCT_PRICE < $100 THEN
SALES_TAX = PRODUCT_PRICE * 0.25
ENDIF
ENDIF
Sequential Cohesion
- The instruction inside a modules are related to each other through the input data.
- The first instruction acts on the data that are passed into the module, the second instruction use the output of the first instruction as its input and so on.
- Sequence of events is very important.
Example.We are taking example of the modules that will calculate TOTAL_PURCHASES first, then use the variable TOTAL_PURCHASES in the subsequent calculation of AMT_DUE function.
PROCESS PURCHASES
TOTAL_PURCHASES = 0
READ NO_OF_PURCHASES
DO LOOP_INDEX = 1 TO NO_OF_PURCHASES
GET_PURCHASES
ADD PURCHASES TO TOTAL_PURCHASES
ENDDO
SALES_TAX = TOTAL_PURCHASES * SALES_TAX_PERCENT
AMT_DUE = TOTAL_PURCHASES + SALES_TAX
END
Communicational Cohesion
- The activities are related to each other by the data that the modules uses.
- Each instruction acts on the same input data or is concerned with the same output data.
- Sequence is not important
Example. Elements of module operate on data that necessary to produce ERR_REPORT
VALIDATE_PRODUCT_REC
IF_TRAN_TYPE NOT = '0' THEN
WRITE_ERR_REPORT
END IF
IF CUST_NONOT NUMERIC THEN
WRITE ERR_REPORT
END IF
IF PRODUCT_NO = BLANKS
WRITE ERR_REPORT
END IF
END
Procedural Cohesion
- The instruction in a module are related to each other through flow of control.
- The instruction are grouped together because of a particular procedural order.
- Sequence is important.
- The instruction are more related to each other modules than they are to each other.
Example. 'AND' word means which module perform more than 1 function.
READ_STUD_REC_AND_TOTAL_STUDENT_Ages
NO_OF_Rec = 0
TOTAL_AGE = 0
READ STUD_REC
DO WHILE MORE_REC_EXIST
ADD AGE TO TOTAL_AGE
ADD 1 TO NO_OF_REC
READ STUD_REC
ENDDO
END
Temporal Cohesion
- The instruction in a module are related to each other through flow of control.
- The instruction are grouped together because they occur at about the same point in time.
- These instructions perform more than one functions.
- e.g. the initializing module at the beginning of program which contain initialization step without affecting other modules.
INITIALIZARION
OPEN TRAN_FILE
ISSUE PROMPT 'ENTER DATE - DDMMYY
READ TODAYS_DATE
SET TRAN_COUNT TO ZERO
SET REPORT_TOTAL TO ZERO
OPEN REPORT_FILE
END
Logical Cohesion
- The instruction are hardly related to each other at all.
- The instruction are grouped together due to certain classes.
- A flag that is passed from outside will determine which set of instruction is to be executed.
Example. "READ_ALL_FILES" is the class - three functions group together due to certain class of activities.
READ_ALL_FILES
CASE OF FILE_CODE
CASE OF FILE_CODE
1: READ CUST_TRAN REC
IF NOT EOF
INCREMENT CUST_TRAN_COUNT
ENDIF
2: READ CUST_MASTER REC
IF NOT EOF
INCREMENT CUST_MASTER_COUNT
ENDIF
3: READ PRODUCT_MASTER REC
IF NOT EOF
INCREMENT PRODUCT_MASTER_COUNT
ENDIF
ENDCASE
END
Coincidental Cohesion
- The instruction has no relationship to each other at all, they just coincidental fall in the same module.
- It is the worst type of cohesion.
Example. No meaningful relationship to each other, just coincidentally fall in same module.
FILE PROCESSING
OPEN EMPLOYEE UPDATE FILE
READ EMPLOYEE REC
PRINT_PAGE_HEADING
OPEN MASTER FILE(EMPLOYEE)
SET PAGE_COUNT TO 1
SET ERR_FLAG TO FALSE
END
0 Comments