Glossary
Code complexity measures how difficult code is to understand and maintain—high complexity creates ongoing maintenance burden and hides risks.
Across three companies — Shiksha Infotech, UshaOm, and Salesken — I've seen the same engineering challenges repeat. The details change but the patterns don't.
By the Glue Team
Code complexity measures how difficult code is to understand, test, and modify. Complex code is harder to understand, more prone to bugs, takes longer to maintain, and requires more testing. Measuring and reducing complexity improves code quality and developer productivity.
Code complexity encompasses multiple dimensions:
Bug Risk: Complex code has more bugs. More paths mean more edge cases. Each edge case is a potential bug.
Maintenance Cost: Complex code takes longer to modify. Simple changes require understanding complex logic.
Testing Difficulty: Complex code requires more tests to cover all paths. Test coverage is harder to achieve.
Onboarding: New developers struggle with complex code. They can't modify it confidently.
Refactoring Risk: Refactoring complex code is risky without tests. Changes easily break other parts.
"Complexity is just about code length." False. A 10-line function with multiple decisions is more complex than a 50-line function that's straightforward.
"Simple code always looks simple." False. Poor naming makes simple code seem complex.
"Complex problems require complex code." False. Good design solves complex problems with simple code.
Code Health: Complexity is one dimension.
Technical Debt: Complex code is a form of debt.
Refactoring: Reducing complexity without changing behavior.
Cyclomatic complexity counts the number of linearly independent paths through a program. Each if, for, while, case, and catch adds one to the count. A function with no branches has cyclomatic complexity of 1. A function with one if statement has complexity of 2.
Benchmarks:
Cognitive complexity measures how hard code is for humans to understand. Unlike cyclomatic complexity, it penalizes nested structures more heavily. A deeply nested if inside a for inside a try scores higher than three sequential if statements, even though the cyclomatic complexity might be the same.
This better reflects the actual difficulty humans face reading code.
Halstead metrics measure complexity based on operators and operands. They calculate:
The simplest metric. Not very accurate — 50 lines of clear code is less complex than 10 lines of nested ternary operators. But LOC correlates loosely with other complexity metrics and is easy to track.
Different languages have different complexity profiles:
| Language | Typical Sources of Complexity | Tools |
|---|---|---|
| JavaScript/TypeScript | Callback chains, async/await, dynamic typing | ESLint, SonarQube |
| Python | Dynamic typing, metaclasses, decorators | Radon, Pylint, SonarQube |
| Java | Deep inheritance, generics, annotations | PMD, SpotBugs, SonarQube |
| Go | Error handling patterns, goroutines | gocyclo, golangci-lint |
| Rust | Lifetime annotations, trait bounds, macros | clippy |
| C/C++ | Pointer arithmetic, preprocessor macros, templates | cppcheck, SonarQube |
in my experience, Microsoft and Google shows concrete impacts:
Break large functions into smaller ones. Each function should do one thing. If a function has complexity above 10, look for opportunities to extract methods.
If you have long switch or if/else chains, consider using polymorphism (strategy pattern, visitor pattern). This moves complexity from conditional logic to class structure, which is easier to extend and test.
Complex boolean conditions are a major source of cognitive complexity. Extract them into named variables or functions: isEligibleForDiscount() is clearer than (user.age > 65 || user.membership === 'gold') && !user.suspended && order.total > 50.
Use early returns (guard clauses) to reduce nesting. Instead of nesting if statements three levels deep, check for invalid conditions first and return early.
Classes with many responsibilities accumulate complexity. Apply the Single Responsibility Principle: split classes so each handles one concern.
For product managers and engineering leaders, code complexity is not just a developer concern:
Tools like Glue can surface code complexity metrics automatically, helping non-technical stakeholders understand why certain features take longer than expected.
Q: What cyclomatic complexity is acceptable? A: Under 10 is good. 10-20 is acceptable. Above 20 is concerning. Use as guide, not rule.
Q: How do you reduce complexity? A: Break functions into smaller functions. Extract decision logic. Use simpler algorithms. Improve naming.
Q: Should you reduce all complexity? A: No. Some complexity is necessary. Reduce complexity in frequently-changing code. Stable complex code can be left alone.
Keep reading
Related resources