Improving code readability - Time & Space Complexity
When we improve code readability, we want to make the code easier to understand and maintain.
But how does this affect how long the program takes to run?
Analyze the time complexity of the following code snippet.
#include <stdio.h>
int sumArray(int arr[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return sum;
}
int main() {
int data[] = {1, 2, 3, 4, 5};
int result = sumArray(data, 5);
printf("Sum: %d\n", result);
return 0;
}
This code adds all numbers in an array and prints the total.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that adds each element of the array.
- How many times: It runs once for each element in the array, so n times.
As the array gets bigger, the number of additions grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the size of the input.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of items.
[X] Wrong: "Making code more readable will always slow it down."
[OK] Correct: Improving readability usually does not add extra loops or heavy work, so it does not change how long the program takes to run.
Understanding how code changes affect time helps you write clear and efficient programs, a skill valued in real projects and interviews.
"What if we added a nested loop inside the for-loop to compare each element with every other? How would the time complexity change?"
Practice
Solution
Step 1: Understand variable naming purpose
Meaningful names describe what the variable holds, making code clearer.Step 2: Consider code readability impact
Clear names help anyone reading the code understand its purpose quickly.Final Answer:
It makes the code easier to understand for others and yourself. -> Option BQuick Check:
Meaningful names improve readability [OK]
- Using single letters without context
- Using vague names like temp or data
- Ignoring naming conventions
Solution
Step 1: Check spacing around operators and keywords
if (x > 0) { printf("Positive"); } uses spaces after if, around operators, and braces properly.Step 2: Check consistent brace placement
if (x > 0) { printf("Positive"); } places braces with spaces, improving readability.Final Answer:
if (x > 0) { printf("Positive"); } -> Option AQuick Check:
Proper spacing and braces improve readability [OK]
- Skipping spaces after keywords
- Putting braces on the same line without spaces
- Omitting braces for single statements
int main() {
int a = 5, b = 10;
int sum = a + b;
printf("Sum is %d\n", sum);
return 0;
}Solution
Step 1: Understand variable initialization and addition
Variables a and b are assigned 5 and 10, sum is their addition (5 + 10 = 15).Step 2: Analyze printf output
printf prints "Sum is " followed by sum's value, which is 15.Final Answer:
Sum is 15 -> Option AQuick Check:
5 + 10 = 15 output [OK]
- Concatenating integers as strings
- Forgetting to add variables
- Syntax errors in printf format
int main(){int x=10;int y=20;if(x>y){printf("x is greater\n");}else{printf("y is greater\n");}return 0;}Solution
Step 1: Analyze current formatting
Code is cramped with no spaces or line breaks, making it hard to read.Step 2: Suggest formatting improvements
Adding spaces and line breaks separates statements and improves readability.Final Answer:
Add spaces and line breaks for clarity. -> Option DQuick Check:
Proper formatting improves readability [OK]
- Ignoring formatting leads to hard-to-read code
- Using goto increases complexity
- Making names less descriptive
float area(float w, float h){return w*h;}How can you improve this code's readability for a team project?
Solution
Step 1: Review current function
Function uses short parameter names and no comments, which may confuse others.Step 2: Suggest improvements
Using descriptive names like width and height and adding comments clarifies purpose.Final Answer:
Add descriptive comments and use meaningful parameter names. -> Option CQuick Check:
Comments and clear names improve teamwork [OK]
- Using vague parameter names
- Skipping comments
- Making code too compact
Ready to prove it?
Pass the test and this pattern moves unknown
