High Cohesion
A software design principle where elements within a module are strongly related and focused on a single, well-defined purpose.
High Cohesion
High cohesion is a fundamental software design principle that complements loose coupling by ensuring that elements within a module or component work together to achieve a single, well-defined purpose. This principle promotes maintainability, reusability, and system clarity.
Core Characteristics
-
Functional Focus
- Each module has a clear, singular responsibility
- Elements within the module directly support its primary purpose
- Follows the single responsibility principle
- Minimal overlap with unrelated functionalities
-
Internal Relationships
- Strong interconnections between module elements
- Shared state and behavior are logically related
- Clear dependencies within the module boundary
- Efficient information hiding implementation
Types of Cohesion
Functional Cohesion (Strongest)
- All elements contribute to a single well-defined task
- Optimal form of cohesion
- Highly maintainable and reusable
- Examples: string manipulation classes, mathematical utilities
Sequential Cohesion
- Elements form a chain of sequential operations
- Output from one element feeds into another
- Common in data processing modules
- Example: read → validate → transform → save
Communicational Cohesion
- Elements operate on the same data set
- Share common inputs or outputs
- Related to data encapsulation
- Example: customer data management functions
Temporal Cohesion (Weaker)
- Elements are related by timing
- Executed together but possibly unrelated
- Often seen in initialization routines
- Example: application startup procedures
Benefits
-
Maintainability
- Easier to understand and modify
- Localized changes
- Reduced technical debt
- Simplified debugging
-
Reusability
- Well-defined module boundaries
- Clear interfaces
- Portable functionality
- Support for modular programming
-
Reliability
- Focused testing
- Reduced side effects
- Better error isolation
- Enhanced quality assurance
Implementation Guidelines
-
Module Design
- Keep classes focused and small
- Group related functionality
- Separate unrelated concerns
- Follow SOLID principles
-
Code Organization
- Logical grouping of methods
- Clear naming conventions
- Consistent abstraction levels
- Proper package design
-
Refactoring Signals
- Large classes or methods
- Mixed levels of abstraction
- Unrelated functionality
- Complex dependencies
Best Practices
- Apply the single responsibility principle strictly
- Use meaningful names that reflect purpose
- Keep classes focused and manageable
- Regularly review and refactor
- Document module responsibilities
- Maintain consistent abstraction levels
Common Anti-patterns
- God Classes (violation of high cohesion)
- Feature Envy
- Shotgun Surgery
- code smells
Relationship with Other Principles
- separation of concerns
- dependency injection
- interface segregation principle
- composition over inheritance
High cohesion, when combined with loose coupling, forms the foundation of robust software design. It enables developers to create maintainable, scalable systems while reducing complexity and technical debt. The principle continues to be essential in modern software architecture, from monolithic applications to microservices architecture.