What is the difference between cohesion and coupling?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

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

  1. High Cohesion:
    • A module or class has a focused and clearly defined purpose.
    • Encourages easier maintenance and reusability.
    • Promotes the Single Responsibility Principle (SRP).
  2. 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

  1. Low Coupling:
    • Components interact with minimal dependencies.
    • Changes in one module are less likely to affect others.
    • Enhances flexibility and testability.
  2. 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

AspectCohesionCoupling
DefinitionMeasures how well the elements of a module work together.Measures the degree of dependency between modules.
GoalFocuses on creating well-defined, focused modules.Focuses on reducing interdependencies between modules.
Ideal StateHigh cohesion is desirable.Low coupling is desirable.
Impact on MaintenanceHigh cohesion simplifies maintenance.Low coupling reduces ripple effects from changes.
RelationInternal 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.
TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How does microservices architecture promote continuous integration and continuous deployment (CI/CD)?
Refinement techniques for partial solutions under interview time limits
How many coding questions per day?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.