What is the difference between cohesion and coupling?
Difference Between Cohesion and Coupling
Cohesion and coupling are fundamental concepts in software design, reflecting the quality of a system’s internal structure and the interdependence between its components. Both are crucial for creating maintainable, scalable, and robust software.
Cohesion
Definition
Cohesion measures how closely related and focused the responsibilities of a single module or component are. High cohesion means that a module is well-defined and performs a single, clear task.
Characteristics
- High Cohesion:
- A module or class has a focused and clearly defined purpose.
- Encourages easier maintenance and reusability.
- Promotes the Single Responsibility Principle (SRP).
- Low Cohesion:
- A module or class handles multiple, unrelated responsibilities.
- Increases complexity and reduces clarity.
Example
High Cohesion:
class FileHandler: def read_file(self, filepath): # Reads the file and returns its contents pass def write_file(self, filepath, data): # Writes data to the file pass
The FileHandler
class is cohesive because its methods are related to file operations.
Low Cohesion:
class Utility: def read_file(self, filepath): pass def send_email(self, recipient, message): pass def calculate_discount(price, percentage): pass
This Utility
class is not cohesive because its methods handle unrelated responsibilities (file handling, emailing, and discount calculation).
Coupling
Definition
Coupling measures the degree of dependency between different modules or components in a system. Low coupling indicates that components are independent, while high coupling means components are tightly dependent on one another.
Characteristics
- Low Coupling:
- Components interact with minimal dependencies.
- Changes in one module are less likely to affect others.
- Enhances flexibility and testability.
- High Coupling:
- Modules are highly interdependent.
- Changes in one module can propagate errors to others.
- Makes the system harder to maintain and extend.
Example
Low Coupling:
class EmailService: def send_email(self, recipient, message): # Sends an email pass class NotificationManager: def __init__(self, email_service): self.email_service = email_service def notify_user(self, recipient, message): self.email_service.send_email(recipient, message)
Here, NotificationManager
depends on the EmailService
interface, making it loosely coupled. If the implementation of EmailService
changes, NotificationManager
remains unaffected.
High Coupling:
class NotificationManager: def notify_user(self, recipient, message): # Directly implements email-sending logic print(f"Sending email to {recipient}: {message}")
Here, NotificationManager
is tightly coupled to the email-sending logic, making it harder to test or modify.
Key Differences
Aspect | Cohesion | Coupling |
---|---|---|
Definition | Measures how well the elements of a module work together. | Measures the degree of dependency between modules. |
Goal | Focuses on creating well-defined, focused modules. | Focuses on reducing interdependencies between modules. |
Ideal State | High cohesion is desirable. | Low coupling is desirable. |
Impact on Maintenance | High cohesion simplifies maintenance. | Low coupling reduces ripple effects from changes. |
Relation | Internal to a module. | Between different modules or components. |
Balancing Cohesion and Coupling
- High Cohesion + Low Coupling: The ideal design where components are focused and loosely connected.
- High Cohesion + High Coupling: Components are well-defined but too dependent on each other, reducing flexibility.
- Low Cohesion + Low Coupling: Components are independent but unfocused, resulting in unclear responsibilities.
- Low Cohesion + High Coupling: The worst-case scenario, leading to a tightly interconnected system with poorly defined components.
Summary
- Cohesion is about how well the internal elements of a module belong together (focus).
- Coupling is about how much a module depends on other modules (interdependence). Striving for high cohesion and low coupling is a hallmark of good software design, ensuring easier maintenance, reusability, and scalability.
GET YOUR FREE
Coding Questions Catalog