Cohesion vs Coupling – Separate Concerns
We mention Cohesion vs Coupling when we are talking about Software Design Principles. Those two principles were first introduced in the book Structured Design: Fundamentals of a discipline of computer program and system design many years ago, even though the principles were around Structured Programming, they are still valid in the Object Oriented world.
Cohesion represents the degree of the systematic or logical connection or consistency in the logic expressed in the our code. In other words, it’s the level of balance and harmony of the software logic within a module.
We want our code to be highly cohesive, as the highly cohesive modules favor maintenance and reusability. On the other hand low cohesion makes it difficult to understand the purpose of the module. This would then lead to the code that is not easy to change and error prone.
- Decreasing cohesion: leads to modules where responsibilities (actions, methods, behavior) have very little in common.
- Increasing cohesion: leads to modules where the responsibility is very well-defined.
The cohesion is very related to the “Single Responsibility Principle”, which says that the “class” should have just one reason to change, which in other words means, that the class should have only one responsibility. A class with multiple responsibilities has more reasons to change.
Coupling instead represents the measure of the level of dependency between two software modules. Coupling could be Low or High, and we definitively want the lower coupling between our modules.
Separation of Concerns
Separation of Concerns is a principle that help us achieving the High Cohesion and Low Coupling .
Separation of Concerns suggests that we should focus on one particular “concerns” at a time and therefore design our classes accordingly.
Low Coupling and High Cohesion are as you may see very related to each other. Both leads to a better and less fragile systems where the maintainability, testing and good reuse are favoured. Separation of Concerns is a principle or mechanism that would help us achieving this goal.