Coupling and cohesion in Software Engineering - Time & Space Complexity
When we look at coupling and cohesion, we want to understand how the design of parts affects the work needed to change or maintain a system.
We ask: How does the way parts connect or focus affect the effort as the system grows?
Analyze the time complexity of interactions between modules with different coupling and cohesion.
// Module A calls Module B's function
function moduleA(n) {
for (let i = 0; i < n; i++) {
moduleB(i);
}
}
function moduleB(x) {
// Does a simple task
return x * 2;
}
This code shows one module repeatedly calling another, illustrating coupling between them.
Look for repeated calls or loops that cause work to grow.
- Primary operation: The loop in moduleA calling moduleB.
- How many times: The loop runs n times, so moduleB is called n times.
As n grows, the number of calls grows directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to moduleB |
| 100 | 100 calls to moduleB |
| 1000 | 1000 calls to moduleB |
Pattern observation: The work grows in a straight line as input size increases.
Time Complexity: O(n)
This means the work grows directly with the size of the input or number of calls.
[X] Wrong: "If modules are tightly connected, the time to run will always be much slower."
[OK] Correct: Tight connection (coupling) affects how easy it is to change code, not always how fast it runs.
Understanding how parts of a system depend on each other helps you explain design choices clearly and shows you think about maintainability and growth.
"What if moduleB itself called another module inside a loop? How would that affect the overall time complexity?"
Practice
low coupling in software design mean?Solution
Step 1: Understand coupling concept
Coupling measures how much modules depend on each other.Step 2: Interpret low coupling
Low coupling means modules have few dependencies, making them independent.Final Answer:
Modules have few dependencies on each other -> Option AQuick Check:
Low coupling = few dependencies [OK]
- Confusing low coupling with high dependency
- Thinking low coupling means modules do many tasks
- Mixing coupling with cohesion concepts
high cohesion in a module?Solution
Step 1: Understand cohesion concept
Cohesion measures how focused a module is on a single task.Step 2: Interpret high cohesion
High cohesion means the module focuses on one clear, well-defined task.Final Answer:
The module focuses on a single, well-defined task -> Option CQuick Check:
High cohesion = single focused task [OK]
- Thinking high cohesion means many unrelated tasks
- Confusing cohesion with coupling
- Assuming high cohesion means high dependency
Solution
Step 1: Analyze Module A and B relationship
Module A calls Module B frequently and shares many variables, indicating high coupling.Step 2: Analyze Module C's task focus
Module C performs one clear task independently, indicating high cohesion.Final Answer:
Module A has high coupling and Module C has high cohesion -> Option BQuick Check:
High coupling = many dependencies; High cohesion = focused task [OK]
- Mixing which module has high coupling
- Confusing cohesion with coupling
- Assuming independence means low cohesion
Solution
Step 1: Identify tasks in the module
The module handles user input, processes data, and generates reports, which are different tasks.Step 2: Assess cohesion level
Because it does many unrelated tasks, the module has low cohesion.Final Answer:
The module has low cohesion -> Option AQuick Check:
Multiple unrelated tasks = low cohesion [OK]
- Confusing low cohesion with low coupling
- Thinking multiple tasks mean high cohesion
- Ignoring the definition of cohesion
Solution
Step 1: Identify current design issues
Modules share many variables (high coupling) and perform unrelated tasks (low cohesion).Step 2: Apply good design principles
Good design aims for low coupling (less dependency) and high cohesion (focused tasks).Final Answer:
Decrease coupling and increase cohesion -> Option DQuick Check:
Good design = low coupling + high cohesion [OK]
- Thinking increasing coupling improves design
- Confusing cohesion with coupling
- Ignoring the benefits of modular design
Ready to prove it?
Pass the test and this pattern moves unknown
