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?"