Cohesion (computer science)
In computer programming, 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 the class's methods and data.
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 software traits including robustness, reliability, reusability, and understandability. In contrast, low cohesion is associated with undesirable traits such as being difficult to maintain, test, reuse, or understand.
Cohesion is often contrasted with coupling. High cohesion often correlates with loose coupling, and vice versa. The software metrics of coupling and cohesion were invented by Larry Constantine in the late 1960s as part of Structured Design, based on characteristics of “good” programming practices that reduced maintenance and modification costs. Structured Design, cohesion and coupling were published in the article Stevens, Myers & Constantine and the book Yourdon & Constantine. The latter two subsequently became standard terms in software engineering.
High cohesion
In object-oriented programming, a class is said to have high cohesion if the methods that serve the class are similar in many aspects. In a highly cohesive system, code readability and reusability is increased, while complexity is kept manageable.Cohesion is increased if:
- The functionalities embedded in a class, accessed through its methods, have much in common.
- Methods carry out a small number of related activities, by avoiding coarsely grained or unrelated sets of data.
- Related methods are in the same source file or otherwise grouped together; for example, in separate files but in the same sub-directory/folder.
- Reduced module complexity, with fewer operations.
- Increased system maintainability, because logical changes in the domain affect fewer modules, and changes in one module require fewer changes in other modules.
- Increased module reusability, because application developers will find the component they need more easily among the cohesive set of operations provided by the module.
Types of cohesion
Cohesion is a qualitative measure, meaning that the source code is examined using a rubric to determine a classification. Cohesion types, from the worst to the best, are as follows:;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. Example:
/*
Groups: The function definitions
Parts: The terms on each function
- /
;Logical cohesion: Logical cohesion is when parts of a module are grouped because they are logically categorized to do the same thing even though they are different by nature.
;Temporal cohesion: Temporal cohesion is when parts of a module are grouped according to the time in which 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/informational cohesion: Communicational cohesion is when parts of a module are grouped because they operate on the same data.
;Sequential cohesion: Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line.
;Functional cohesion : Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module. Example:
/*
Groups: The function definitions
Parts: The terms on each function
- /
Module B
;Perfect cohesion : Example.
/*
Groups: The function definitions
Parts: The terms on each function
- /
Although cohesion is a ranking type of scale, the ranks do not indicate a steady progression of improved cohesion. Studies by Larry Constantine, Edward Yourdon, and Steve McConnell indicate that the first two types of cohesion are inferior, communicational and sequential cohesion are very good, and functional cohesion is superior.