Cohesion (computer science)
Definition
In the field of computer science, cohesion refers to the degree to which the elements inside a module belong together. In one sense, it is a measure of the strength of relationship between the methods and data of a class and some unifying purpose or concept served by that class. In another sense, it is a measure of the strength of relationship between pieces of functionality within a given module. For example, in highly cohesive systems functionality is strongly related.
Types of Cohesion
There are several levels of cohesion, often expressed as a list of increasing order of desirability:
Coincidental Cohesion
Coincidental cohesion is when parts of a module are grouped arbitrarily; the only relationship between the parts is that they have been grouped together.
Logical Cohesion
Logical cohesion is when parts of a module are grouped because they are logically categorized to do the same thing, even if they are different by nature.
Temporal Cohesion
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution.
Procedural Cohesion
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution.
Communicational Cohesion
Communicational cohesion is when parts of a module are grouped because they operate on the same data (i.e., they work together to achieve an objective).
Sequential Cohesion
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part.
Functional Cohesion
Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task.
Importance of Cohesion
Cohesion is an ordinal type of measurement and is usually described as “high cohesion” or “low cohesion”. Modules with high cohesion tend to be preferable, because high cohesion is associated with several desirable traits of software including robustness, reliability, reusability, and understandability. In contrast, low cohesion is associated with undesirable traits such as being difficult to maintain, test, reuse, or even understand.
Cohesion and Coupling
Cohesion is often contrasted with coupling, another important concept in computer science. High cohesion often correlates with loose coupling, and vice versa. The software quality metrics of coupling and cohesion were invented by Larry Constantine in the late 1960s as part of a structured design, based on characteristics of “good” programming practices that reduced maintenance and modification costs.