0
0
Software Engineeringknowledge~5 mins

Coupling and cohesion in Software Engineering - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Coupling and cohesion
O(n)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As n grows, the number of calls grows directly with it.

Input Size (n)Approx. Operations
1010 calls to moduleB
100100 calls to moduleB
10001000 calls to moduleB

Pattern observation: The work grows in a straight line as input size increases.

Final Time Complexity

Time Complexity: O(n)

This means the work grows directly with the size of the input or number of calls.

Common Mistake

[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.

Interview Connect

Understanding how parts of a system depend on each other helps you explain design choices clearly and shows you think about maintainability and growth.

Self-Check

"What if moduleB itself called another module inside a loop? How would that affect the overall time complexity?"