die Kohäsion (pl. -en) – cohesion
Last week, we talked about coupling, which describes the relationship between different classes.
Cohesion, in contrast, focuses on a single class or module.
It asks the question: What does this class/module do? What are its functions?
If its functions are focused and closely related, then you would say that this class or module has high cohesion. This is what we want.
If a class has one well-defined job, and does only that job, it has high cohesion. This general principle can also apply to methods. It reminds me of how in Clean Code, Robert Martin says a method should only “do one thing”.
On the other hand, if the class is like a jack-of-all-trades and does odd jobs here and there, then it has low cohesion.
As was already mentioned last week, cohesion is typically contrasted with coupling[LINK].
A system with high cohesion within a single class, but low coupling between different classes is a sign that it is well-designed.
The word comes from Latin cohaesum, and is the second participle of the verb cohaerere (which means “to be connected”).
Cohaerere = con- (with) + haerere (to stick, to cling)