Q
Coupling with types
Cohesion with types
Ans
Sure, let’s break down coupling and cohesion in simple terms.
---
Coupling
Coupling is about how much one part of a system relies on other parts. Lower coupling is generally
preferred because it makes the system more flexible and easier to change. If parts of a system are too
dependent on each other, changing one part could break others, which is not ideal.
Types of Coupling:
1. Content Coupling (High Coupling):
One part directly accesses or modifies another part’s internal data.
Example: Function A directly changes the variables inside Function B. If Function B’s variables change,
Function A will break.
2. Control Coupling:
One module controls the behavior of another by passing control information.
Example: Function A tells Function B what to do by passing a flag, which makes B dependent on A’s
structure.
3. Data Coupling (Low Coupling):
Modules communicate by passing only the data they need, not implementation details.
Example: Function A passes only relevant information to Function B, like variables or objects, without
controlling how B uses them.
4. Message Coupling (Very Low Coupling):
Modules interact only through well-defined messages or APIs.
Example: Two web services communicate only through HTTP requests. They don’t need to know each
other’s internal details.
5. No Coupling (Ideal):
Modules are completely independent.
Example: Two separate applications that don’t interact with each other.
---
Cohesion
Cohesion is about how closely related and focused the responsibilities of a single module or component
are. Higher cohesion is preferred because it makes components more understandable, easier to maintain,
and more reusable.
Types of Cohesion:
1. Coincidental Cohesion (Low Cohesion):
Tasks are grouped together randomly and don’t relate to each other.
Example: A function that reads from a file, prints a report, and sends an email. These tasks don’t logically
belong together.
2. Logical Cohesion:
Similar tasks are grouped, but they don’t necessarily relate directly.
Example: A function that performs various math operations. Though they are similar, each operation
could logically be separate.
3. Temporal Cohesion:
Tasks are related by the time they’re executed.
Example: A function that initializes variables and creates a log file when the program starts. These tasks
occur together but aren’t necessarily related.
4. Procedural Cohesion:
Tasks are related and follow a specific sequence.
Example: A function that fetches data, processes it, and displays it on the screen. These steps are related
by the procedure.
5. Sequential Cohesion:
Tasks are related, where the output of one becomes the input for the next.
Example: A function that reads from a file, processes the data, and saves it. Each step directly feeds into
the next.
6. Functional Cohesion (High Cohesion):
All tasks relate to a single, focused purpose.
Example: A function that only handles user login. Every part of this function works toward the single goal
of logging in the user.
---
In Summary:
Coupling refers to the dependency between modules:
Low coupling = independent parts = better flexibility and maintainability.
Cohesion refers to how focused a module’s purpose is:
High cohesion = single, focused purpose = easier to understand and maintain.