Cohesion and Coupling
Clean, responsive HTML conversion using the same format as the previous study-note HTML files.
Source: Converted from the uploaded Word-generated HTML document
Cohesion.htm. The source file contains the study material on
Cohesion and Coupling.
What is Cohesion and Coupling?
Cohesion is about how strongly the components of a single module/class are related to each other.
Coupling is about how dependent one module/class is on another .
| Aspect | Cohesion | Coupling |
|---|---|---|
| Definition | Degree to which elements inside a module belong together Measures how closely related the responsibilities of a single class are. | Degree of interdependence between modules Measures how dependent classes are on each other. |
| Goal | High cohesion is desirable | Low coupling is desirable |
| Focus | Within a class/module | Between classes/modules |
| Improves | Maintainability, readability, testability | Flexibility, reusability, modularity |
| Good Example | A class that performs a single, well-defined task | A class that depends on interfaces, not implementations |
| Bad Example | A class that handles DB, UI, and business logic | A class that directly instantiates and uses other classes |
Key Takeaways
- High cohesion is desirable because responsibilities within a class/module should belong together.
- Low coupling is desirable because classes/modules should have minimal dependency on one another.
- Cohesion focuses on relationships within a class/module.
- Coupling focuses on relationships between classes/modules.
- The source associates cohesion with maintainability, readability and testability.
- The source associates low coupling with flexibility, reusability and modularity.
Cohesion vs Coupling — Quick Reference
| Aspect | Cohesion | Coupling |
|---|---|---|
| Definition | Degree to which elements inside a module belong together; measures how closely related the responsibilities of a single class are. | Degree of interdependence between modules; measures how dependent classes are on each other. |
| Goal | High cohesion is desirable | Low coupling is desirable |
| Focus | Within a class/module | Between classes/modules |
| Improves | Maintainability, readability, testability | Flexibility, reusability, modularity |
| Good Example | A class that performs a single, well-defined task | A class that depends on interfaces, not implementations |
| Bad Example | A class that handles DB, UI, and business logic | A class that directly instantiates and uses other classes |