Beginner LevelSoftware Engineering Principles
What is Cyclomatic Complexity and how does it measure code maintainability?
❓ Interview Prompt: Explain Cyclomatic Complexity metric, calculating execution decision branches, impact on testing, and target complexity limits.
Cyclomatic Complexity is a quantitative software metric measuring the number of linearly independent execution paths through a program's source code:
Formula & Thresholds:
Calculated using decision points ('if', 'while', 'for', 'switch', '&&', '||'):
$$\text{Complexity } V(G) = E - N + 2P$$
•
1 to 10: Simple, low-risk code (Easy to test).
•
11 to 20: Moderately complex (Requires refactoring).
•
> 20: High risk, complex code (Extremely hard to unit test thoroughly!).
Code Implementation Example
typescript
// High Cyclomatic Complexity (5 decision paths)
function process(status: string, age: number, isVip: boolean) {
if (status === 'ACTIVE') {
if (age > 18 || isVip) { /* path 1 & 2 */ }
else { /* path 3 */ }
} else { /* path 4 & 5 */ }
}💡Analyzing decision branches for cyclomatic complexity computation.
🎯 Interview Key Takeaway
Keep cyclomatic complexity under 10 per function to ensure code readability and thorough unit testability.
#Cyclomatic Complexity#Code Metrics#Quality#Beginner
Back to all Software Engineering Principles Questions→