liveBook · Manning

Cohesion

As explained in Microservices Patterns, Second Edition

Cohesion is the degree to which the elements or responsibilities within a software component belong together.

Cohesion is the degree to which the elements within a software component belong together. A cohesive element groups child elements that are tightly coupled, so changes affecting them are more likely to stay within the same package or module. section 4.2 section 4.2.2

Cohesion complements loose design-time coupling: cohesion groups related elements, while loose coupling reduces the likelihood that a change to one element requires changes to another. Apply both goals across the hierarchy, from methods and classes through packages, subdomains, and components. Some coupling is inevitable; the design task is to group and manage it. section 4.2.1

Figure 4.2 You can localize changes, which improves maintainability, by designing loosely coupled packages that are internally cohesive by virtue of being comprised of tightly coupled classes
improving cohesion order menu

The figure shows how tightly coupled classes can form cohesive packages that are loosely coupled to one another. section 4.2.1

Localizing a customer change

Order Management invokes the Customer Management API. If the subdomains are loosely design-time coupled, a Customer Management change is unlikely to require an Order Management change and might even be localized to one class. If they are tightly coupled, the change is more likely to spread, potentially requiring coordination between teams. section 4.2.2

Localized changes are easier and safer to make, improve team autonomy, and can avoid time-consuming changes across components. In a microservice architecture, grouping decisions also create process and transaction boundaries. A design that simplifies an operation may come at the expense of a less desirable service design, so evaluate the alternatives rather than treating cohesion as the only goal. section 5.2.2 section 7.5.3

How Microservices Patterns, Second Edition covers it

How deep it goes
Defines cohesion through tightly coupled child elements and explains its consequences across the software design hierarchy.
Examples
Uses Customer Management and Order Management to illustrate how coupling affects the spread of changes between subdomains.
What you can do after
Evaluate and understand grouping decisions by considering change localization and microservice boundary trade-offs.
How it connects
Connects cohesion with design-time coupling, modifiability, microservice architecture, and distributed monoliths.

Also explained in

Compare how 7 books explain cohesion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.