Cohesion (computer science)
Encyclopedia : C : CO : COH : Cohesion (computer science)
- This page is about the concept in computer programming. For the concept in chemistry, see Cohesion (chemistry).
Cohesion is usually contrasted with coupling. High cohesion often correlates with low coupling, and vice versa. The software quality metrics of coupling and cohesion were defined by Wayne P. Stevens, Glenford J. Myers, and Larry L. Constantine from source code analysis they conducted on several programming projects while at IBM, all in an effort to identify the characteristics of "good" programming practices.
High cohesion
In object-oriented programming, it is good to assign responsibilities to classes in a way that keeps cohesion high. The likelihood of reuse is increased and complexity kept manageable.Cohesion is a measure of how strongly related and focused the responsibilities of a single class are.
Cohesion is decreased if:
- The responsibilities (methods) of a class have little in common.
- Methods carry out many varied activities, often using varied data.
- Difficult to understand.
- Difficult to maintain, because it is constantly affected by changes.
- Difficult to reuse a class because most applications won’t need a random set of operations attached to a class.
Types of cohesion
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a rubric to determine a cohesion classification. The types of cohesion, in order of lowest to highest, are as follows:
- Coincidental cohesion
- Coincidental cohesion is when parts of a module are grouped arbitrarily; the parts have no significant relationship (e.g. a module of frequently used functions).
- Logical cohesion
- Logical cohesion is when parts of a module are grouped because of a slight relation (e.g. using control coupling to decide which part of a module to use, such as how to operate on a bank account).
- 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 (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).
- Procedural cohesion
- Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).
- Communicational cohesion
- Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a method updateStudentRecord which operates on a student record, but the actions which the method performs are not clear).
- Sequential cohesion
- Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part (e.g. a function which reads data from a file and processes the data).
- 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 (a perfect module).
However, while functional cohesion is considered the most desirable type of cohesion for a software module, it may not actually be achievable. There are many cases where communicational cohesion is about the best that can be attained in the circumstances. However the emphasis of a software design should be to maintain module cohesion of communicational or better since these types of cohesion are associated with modules of lower lines of code per module with the source code focused on a particular functional objective with less extranous or unnecessary functionality, and tend to be reusable under a greater variety of conditions.
References
See also
From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.
