Data Structures Notes
Data Structures Notes
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:
Example: Function A directly changes the variables inside Function B. If Function B’s variables change,
Function A will break.
2. Control Coupling:
Example: Function A tells Function B what to do by passing a flag, which makes B dependent on A’s
structure.
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.
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:
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:
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:
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.
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: