In computer programming, cohesion refers to the degree to which each part of a module is associated with each other part, in terms of functional relation. Parts of a module are functionally related if each part is essential to the functionality and the interface of a well-defined module (a well-defined module is one that has a single task, or models a single object). Cohesion can be considered "high" or "low". High cohesion means each part of a module is functionally related, and low cohesion means each part of a module is not. High cohesion of a module is considered better design in a computer system.
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 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).
Cohesion is usually contrasted with coupling. High cohesion often correlates with low coupling, and vice versa.
See also