Part 3 of 7 · 3 min read
Cohesion
Cohesion asks whether the parts of a module belong together.
Think of a kitchen drawer. A drawer for cutlery is cohesive: everything in it is used at the same time for the same purpose. A drawer holding a spoon, a battery, a tape measure and a birthday candle is not.
FSA defines cohesion as the extent to which the parts of a module should be in the same module. It quotes Larry Constantine: trying to divide a cohesive module only raises coupling and lowers readability. If you split it, the pieces must call each other to do anything useful.
Cohesion
Cohesion measures how strongly the parts of one module are related to each other. High cohesion means they belong together.
Example: a module with "add item", "remove item" and "total" for a shopping cart has high cohesion. A module with "send email", "parse date" and "resize image" has low cohesion.
The book lists seven kinds, from best to worst:
| Kind | Meaning | Example |
|---|---|---|
| Functional | Every part is related and the module has all it needs | A tax calculation module |
| Sequential | One part's output is the next part's input | Read file, then parse it |
| Communicational | Parts work on the same data | Save a record and email about the same record |
| Procedural | Parts must run in a set order | Steps in a checkout flow |
| Temporal | Parts are related only by when they run | Start-up tasks that happen to run together |
| Logical | Parts are related by theme but do different things | A StringUtils package of unrelated helpers |
| Coincidental | Nothing relates them except the file | A misc file with random helpers |
The book admits cohesion is less precise than coupling. Take a "Customer Maintenance" module with add, update, get and notify customer, plus "get customer orders" and "cancel customer orders". Should the last two form an "Order Maintenance" module? The book says it depends:
- Are those the only two order operations? If so, merging may be better.
- Will the customer module grow large, so that extracting orders makes sense?
- Would Order Maintenance need so much customer knowledge that splitting forces heavy coupling?
These are trade-off questions, the heart of the architect's job.
There is also a measure of the lack of cohesion, LCOM (lack of cohesion in methods). It counts methods that do not share fields. If a class has fields a and b, and some methods use only a while others use only b, the class is really two classes, and LCOM is high. FSA says it is handy when moving from one style to another, because it finds shared utility classes that were never one thing. It also warns that it only sees structure. It cannot judge whether pieces logically fit together.
A worked example (made up). A class Report has fields title and rows. Methods setTitle, getTitle use only title. Methods addRow, rowCount, exportCsv use only rows.
| Group | Fields used | Methods |
|---|---|---|
| 1 | title | 2 |
| 2 | rows | 3 |
The two groups share no field. The class could be split into a Title and a Table without changing behaviour, so its LCOM is high.
Check yourself:
- Which is the weakest kind of cohesion?
- Why can LCOM not tell whether two classes belong together in the business sense?