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 .

AspectCohesionCoupling
DefinitionDegree 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.
GoalHigh cohesion is desirableLow coupling is desirable
FocusWithin a class/moduleBetween classes/modules
ImprovesMaintainability, readability, testabilityFlexibility, reusability, modularity
Good ExampleA class that performs a single, well-defined taskA class that depends on interfaces, not implementations
Bad ExampleA class that handles DB, UI, and business logicA 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
DefinitionDegree 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.
GoalHigh cohesion is desirableLow coupling is desirable
FocusWithin a class/moduleBetween classes/modules
ImprovesMaintainability, readability, testabilityFlexibility, reusability, modularity
Good ExampleA class that performs a single, well-defined taskA class that depends on interfaces, not implementations
Bad ExampleA class that handles DB, UI, and business logicA class that directly instantiates and uses other classes