Short Definition
Cohesion describes how closely related the responsibilities within a single module or component are. High cohesion means a component has a clear, focused purpose. Low cohesion means it handles unrelated responsibilities.
Extended Definition
Cohesion reflects how well the responsibilities inside a module fit together. A highly cohesive module performs one well-defined task or represents one clear concept. Low cohesion occurs when a component mixes unrelated logic, such as business rules, UI behavior, and data persistence. High cohesion improves readability, testability, and maintainability while reducing the risk that changes in one area affect unrelated functionality.
Cohesion and coupling are complementary concepts. Good architectures aim for high cohesion and low coupling. When cohesion is high, modules are more understandable and less prone to error. When cohesion is low, modules become bloated and fragile.
Deep Technical Explanation
Cohesion appears in several categories.
Functional Cohesion
The ideal form. All elements contribute to one specific task or behavior.
Sequential Cohesion
Parts are related because they execute in a specific sequence, such as data transformation stages.
Communicational Cohesion
Parts use the same data set but may perform different operations.
Procedural Cohesion
Parts must follow a process, but may involve unrelated logic.
Temporal Cohesion
Elements are grouped because they occur at the same time, such as startup tasks.
Coincidental Cohesion
The lowest form. Elements are grouped arbitrarily.
Increasing cohesion often involves refactoring modules into smaller, focused units and separating concerns across clearer boundaries.
Practical Examples
- A controller handling authentication, reporting, and UI formatting is low cohesion
- A service dedicated solely to processing payments shows high cohesion
- A logging module that centralizes all logging responsibility demonstrates good cohesion
- A class containing unrelated utility functions suffers from low cohesion
Why It Matters
High cohesion results in code that is easier to change, test, and understand. It makes modules more predictable and reduces unintended side effects. Low cohesion creates tangled logic and increases maintenance costs.
How BlueGrid.io Uses It
BlueGrid.io improves cohesion by:
- Splitting monolithic modules into focused services
- Applying domain-driven boundaries to group related responsibilities
- Reviewing cohesion during code audits and refactoring efforts
- Encouraging single responsibility principles in team coding practices
- Designing workflows and services that map clearly to business capabilities
This ensures clean, maintainable systems for clients.