Part 4 of 6 · 1 min read
Kinds of cohesion
From strongest to weakest.
FSA lists seven kinds of cohesion, from best to worst. They apply to modules, and the ideas carry down to classes.
| Kind | Meaning | Example |
|---|---|---|
| Functional | Every part contributes to one clear job | A PriceCalculator |
| Sequential | One part's output feeds the next | Parse, then validate |
| Communicational | Parts work on the same data | Save a booking, then email about it |
| Procedural | Parts must run in a set order | Steps of a checkout |
| Temporal | Parts run at the same time | Start-up tasks |
| Logical | Parts are alike in kind but do different things | A StringUtils class |
| Coincidental | Parts share only a file | A Misc class |
The top three are fine. The bottom two are warning signs. FSA names StringUtils as a common example of logical cohesion, a bag of static methods on strings that are otherwise unrelated. A Helper or Misc class usually has coincidental cohesion.
FSA also admits that cohesion is a less precise measure than coupling, and that judging it often depends on the architect's view. A class of "add customer, update customer, get customer" can be seen as one cohesive unit or as three jobs. Use the measures to start a discussion in an interview, and state your reasoning.
Check yourself:
1. Which two kinds are warning signs?
2. Why is StringUtils called logically cohesive?