0
0
Software Engineeringknowledge~5 mins

Design for change and extensibility in Software Engineering - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Design for change and extensibility
O(n)
Understanding Time Complexity

When designing software to handle change and grow over time, it is important to understand how the cost of making changes scales as the system grows.

We want to know how adding new features or modifying parts affects the work needed as the project size increases.

Scenario Under Consideration

Analyze the time complexity of adding new features using a design that supports extensions.


class FeatureManager {
  constructor() {
    this.features = [];
  }

  addFeature(feature) {
    this.features.push(feature);
  }

  runAll() {
    for (const feature of this.features) {
      feature.execute();
    }
  }
}
    

This code manages a list of features that can be added and run. It supports adding new features without changing existing code.

Identify Repeating Operations

Look at the operations that repeat as the number of features grows.

  • Primary operation: Looping through all features in runAll() to execute them.
  • How many times: Once for each feature added, so the loop runs as many times as there are features.
How Execution Grows With Input

As you add more features, the time to run all features grows proportionally.

Input Size (n)Approx. Operations
1010 feature executions
100100 feature executions
10001000 feature executions

Pattern observation: The work grows directly with the number of features added.

Final Time Complexity

Time Complexity: O(n)

This means the time to run all features grows linearly as you add more features.

Common Mistake

[X] Wrong: "Adding more features won't affect performance much because each feature runs quickly."

[OK] Correct: Even if each feature is fast, running many features adds up, so total time grows with the number of features.

Interview Connect

Understanding how design choices affect time complexity helps you build software that stays efficient as it grows, a skill valued in real projects and interviews.

Self-Check

"What if each feature called other features recursively? How would that affect the time complexity?"