Cohesion
Definition
Cohesion is a measure of how closely related and focused the responsibilities of a single module are — how well the elements within one module belong together. Good design aims for HIGH cohesion — each module should do one well-defined thing.
Types of Cohesion (from Worst/Lowest to Best/Highest)
| Type | Description | Cohesion Level |
|---|---|---|
| Coincidental Cohesion | Elements grouped arbitrarily, with no meaningful relationship | Worst (Lowest) |
| Logical Cohesion | Elements grouped because they perform logically similar tasks, selected by a flag/parameter (e.g., a single module doing all "print" operations for different formats) | Low |
| Temporal Cohesion | Elements grouped because they execute at the same time (e.g., an "initialization" module that sets up several unrelated things) | Low-Moderate |
| Procedural Cohesion | Elements grouped because they follow a certain sequence of execution, even if functionally unrelated | Moderate |
| Communicational Cohesion | Elements grouped because they operate on the same data | Moderate-High |
| Sequential Cohesion | Output of one element is the input to the next, forming a pipeline within the module | High |
| Functional Cohesion | All elements contribute to a single, well-defined task | Best (Highest) |
Coincidental < Logical < Temporal < Procedural < Communicational < Sequential < Functional
(Worst) (Best)
Why High Cohesion Matters
- Modules are easier to understand (single clear purpose)
- Easier to test in isolation
- Easier to reuse — a cohesive module does one thing well
- Changes are localized — modifying one responsibility doesn't affect unrelated code
Relationship: Coupling vs. Cohesion
Golden Rule of Good Design: "Low Coupling, High Cohesion"
| Coupling | Cohesion | |
|---|---|---|
| Measures | Interdependence between modules | Relatedness within a module |
| Goal | Minimize (low) | Maximize (high) |
Exam tip: Memorize cohesion types from worst to best: Coincidental → Logical → Temporal → Procedural → Communicational → Sequential → Functional. Almost always asked together with Coupling as a combined question — state the "Low Coupling, High Cohesion" principle explicitly.