|
In this article:
- What is Cohesion
- Degrees of Cohesion
- Types of Cohesion
What is Cohesion?
The term cohesion refers to the degree to which the elements of a single module of a program are bound to each other. This is a measure of how closely the lines of code in a particular module work with each other to achieve a particular degree of functionality. Cohesion, as a measure of quality of software, is measured as being ‘high’ or low, which indicated that it is ordinal.
High cohesion is preferrable to low cohesion as it means that particular software is robust, efficient, easily understandable and highly reliable. These are some of the most desirable qualities of software. Low cohesion often means problems in maintaning, testing and understanding of software (or its part). Originally invented by Wayne P. Stevens, Glenford J. Myers, and Larry L. Constantine at IBM, it is now a standard way of measuring the quality of a software.
A related concept is that of coupling which is seen as an opposite to cohesion. It is known that high degree of cohesion means low degree of coupling and the exact reverse is also true.
Degree of Cohesion
Cohesion may be either high or low. A high degree of cohesion is desirable in system or programs to make it easy to understand as well as more reliable, as already explained. Taking an example of cohesion from object-oriented programming, where a class is given a set of responsibilities alongwith necessary inputs (such as variables and functions), there is high degree of cohesion. This means that now the same code can be used ‘n’ number of times in the program without having to declare it again. So also, the program is very simple rather than being complex.
There are certains disadvantages of low cohesion that make high cohesion desirable:
- The code becomes difficult to understand.
- Since the whole code is affected by changes in one part of the program, there is a rise in maintainence issues.
- Polymorphism becomes higly difficult.
Types of cohesion
It is important to know that cohesion is a qualitative measure. It cannot be measured in units. Rather, it is expressed as being “High” or “Low” when discussing it. There are certain types of cohesion. They are explained below in an ascending order of degree (low to high):
- Coincidental Cohesion
‘Coincidently’ means ‘by chance’. This, coincidental cohesion is a situation in which the modules of a program are divided into sub-parts (say, after every nth line). This is said to be the worst type of cohesion and occurs when the concepts of modular programming and cohesion are not clear to the programmer.
- Logical Cohesion
When modules of a program are grouped together due to a small relationship between them, it is called Logical cohesion. This can be seen when two or more modules are having some code in common. When this type of cohesion is encountered, it is important to split such a logically cohesive module into smaller parts.
- Temporal Cohesion
Sometimes, it so happens that certain procedures or functions are performed at the same time are grouped and put inside a single module. When this happens, all the procedures that are to be executed simultaneously are found in a single module. An example is that of the opening of a file in a program and the exception handler that tells the user if the opening of the file was unsuccessful.
- Procedural Cohesion
Sometimes, related functions maybe put into the same module, say, when they are to be executed in a sequence that is repetitive. This achieves what is called Procedural cohesion.
- Communicational Cohesion
Communicational cohesion is when the parts of the same module act or work upon the same data.
- Functional Cohesion
This is the highest degree of cohesion and so also the most desirable type of cohesion. When a module is functionally cohesive, it signifies that all the parts of the module are put together into it because they perform as single, well-defined function or task. Such a module is then called a perfect module. It is to be noted that it may not always be possible to have functional cohesion in a program or module.
|