Separation of concerns in Software Engineering - Time & Space Complexity
When we separate concerns in software, we divide tasks into parts that handle specific jobs. This helps us understand how the work grows as the program gets bigger.
We want to see how splitting tasks affects the time it takes to run the program.
Analyze the time complexity of the following code snippet.
function processData(data) {
const cleaned = cleanData(data); // cleans input data
const analyzed = analyzeData(cleaned); // analyzes cleaned data
return generateReport(analyzed); // creates report from analysis
}
function cleanData(items) {
return items.filter(item => item !== null);
}
function analyzeData(items) {
let sum = 0;
for (const item of items) {
sum += item.value;
}
return sum / items.length;
}
function generateReport(average) {
return `Average value is ${average}`;
}
This code separates tasks: cleaning data, analyzing it, and making a report.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over the data items in
analyzeData. - How many times: Once for each item in the input array.
As the number of data items grows, the time to clean and analyze grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 (clean + analyze each item) |
| 100 | About 200 |
| 1000 | About 2000 |
Pattern observation: The work grows in a straight line with the number of items.
Time Complexity: O(n)
This means the time to run grows directly in proportion to the number of data items.
[X] Wrong: "Separating concerns always makes the program slower because it adds extra steps."
[OK] Correct: Separating concerns organizes work clearly but does not necessarily add extra loops or slow down the main operations.
Understanding how dividing tasks affects performance helps you explain design choices clearly and shows you think about both code quality and efficiency.
"What if the analyzeData function called another loop inside its loop? How would the time complexity change?"