Cohesion
- In Turkish
- Uyum
- Pronunciation
- koh-HEE-zhun
In short
Cohesion is a measure of how closely the responsibilities inside a module, class, or service belong together, and high cohesion is a sign of good design.
What is cohesion in software design?
Cohesion describes how strongly the parts of a single module relate to each other. A class with high cohesion does one well-defined job, and all its methods and data serve that job, like an InvoiceCalculator that only computes totals, taxes, and discounts. A class with low cohesion is a grab bag of unrelated tasks, like a Utils class that formats dates, sends emails, and resizes images.
The idea comes from structured design in the 1970s, which ranked kinds of cohesion from weakest to strongest. Coincidental cohesion, where things are grouped for no real reason, is the worst, and functional cohesion, where everything contributes to a single task, is the best. Warning signs of low cohesion include vague names like Manager or Helper, methods that use completely different fields, and a class that has to change for many unrelated reasons; the single responsibility principle in SOLID is essentially a rule for high cohesion.
A well-organized kitchen is a good analogy: one drawer holds only cutlery and another only baking tools, so you know exactly where to look. The junk drawer, full of batteries, rubber bands, and old keys, is low cohesion. The same idea applies to services, where grouping code by business capability, such as billing or shipping, gives each service a clear purpose.
Cohesion is often confused with coupling, and the two are usually discussed together. Cohesion is about how well the things inside one module belong together, while coupling is about how much separate modules depend on each other, and the goal is high cohesion combined with loose coupling. High cohesion also doesn't simply mean small: splitting a cohesive class into many tiny ones can scatter a single responsibility across files and increase coupling.
Key takeaways
- Cohesion measures how well a module's contents belong together.
- High cohesion means one clear responsibility; low cohesion means a grab bag.
- The single responsibility principle is a rule for achieving high cohesion.
- Aim for high cohesion inside modules and loose coupling between them.
- Cohesive doesn't mean tiny; split by responsibility, not by size.
Example
// Low cohesion: unrelated jobs living in one class
class AppHelper {
formatDate(d: Date) { /* ... */ }
sendWelcomeEmail(to: string) { /* ... */ }
resizeImage(file: Blob) { /* ... */ }
}
// High cohesion: each class has one clear purpose
class DateFormatter { format(d: Date) { /* ... */ } }
class WelcomeMailer { send(to: string) { /* ... */ } }
class ImageResizer { resize(file: Blob) { /* ... */ } }Readers ask
What is the difference between coupling and cohesion?
Cohesion describes how closely related the responsibilities inside one module are, while coupling describes how much different modules depend on each other. Good designs combine high cohesion within modules with loose coupling between them.
How do you measure cohesion?
Informally, check whether a module has one clear purpose you can describe in a sentence and whether its methods work with the same data. Static analysis tools can also compute metrics such as LCOM (lack of cohesion of methods), which flags classes whose methods share few fields.
How is cohesion related to the single responsibility principle?
The single responsibility principle says a class should have only one reason to change. Following it naturally produces high cohesion, because everything in the class serves the same responsibility.
See also
- Loose CouplingSoftware Architecture, p. 26Loose coupling is a design principle in which components depend on each other as little as possible, so one can change without breaking the others.
- SOLIDSoftware Architecture, p. 42SOLID is a set of five object-oriented design principles that help developers write code that is easier to understand, extend, test, and maintain.
- Separation of ConcernsSoftware Architecture, p. 37Separation of concerns is a design principle that divides a program into distinct parts, each responsible for one clearly defined aspect of its behavior.
- RefactoringSoftware Architecture, p. 33Refactoring is the process of restructuring existing code to make it cleaner and easier to maintain without changing what the code does from the outside.
- Domain-Driven DesignSoftware Architecture, p. 15Domain-driven design is an approach to building software that models the code closely on the business domain, using the same language as the domain experts.
- MicroservicesSoftware Architecture, p. 27Microservices are an architectural style where an application is split into small, independently deployable services that communicate over a network.
Spotted a mistake or something missing on this page?Suggest an edit