Coupling and Cohesion

Two relevant concepts in the development of any software.
The coupling and the cohesion:
Coupling:
We can define it as the degree to which a class, method or any other software entity, is directly linked to another. This degree of coupling can also be seen as a degree of dependence.
  • example: when we want to use a class that is tightly bound (has a high coupling) to one or more classes, we will end up using or modifying parts of these classes for which we are dependent.
Cohesion:
Cohesion is the measure in which two or more parts of a system work together to obtain better results than each part individually.
  • example, a class that parses both dates and URLs is not coherent, because they’re unrelated concepts.
  • At the other extreme, a class that parses only the punctuation in a URL is unlikely to be coherent, because it doesn’t represent a whole concept. To get anything done, the programmer will have to find other parsers for protocol, host, resource, and so on. Features with “high” coherence are easier to maintain.
To obtain a good software we must always try to have a low coupling and a high cohesion, and SOLID principles help us with this task. If we follow these guidelines our code will be more robust, maintainable, reusable and extensible and we will avoid the tendency of our code to break in many places every time something is changed.



Comments