95th Percentile Complexity
complexity_p95 — The complexity score exceeded by only 5% of functions
What It Measures
complexity_p95 is the complexity score at the 95th percentile of all functions in your repository. This means 95% of your functions have a complexity score at or below this value, and only 5% exceed it.
This metric gives you the "worst typical" complexity — it tells you how complex your most challenging functions are, while filtering out rare outliers that might be one-off edge cases (generated code, legacy functions, etc.).
Why It Matters
Better Alerting Than Max
complexity_max can be permanently elevated by a single generated or legacy function that nobody plans to refactor. P95 is more practical for alerting because it reflects the complexity level that a meaningful portion of your code reaches. If P95 is climbing, your top 5% is getting worse — that is actionable.
Tracks the "Hard" Code
The average can mask problems — you could have 100 simple functions and 5 extremely complex ones, and the average would look fine. P95 specifically targets the functions that developers dread working on.
Resistant to Noise
Adding many small utility functions does not reduce P95 (unlike the average). P95 only improves when you actually reduce the complexity of your most complex functions.
How It's Calculated
Calculation
Example: If you have 200 functions and the 190th highest complexity score is 12, then complexity_p95 = 12. This means 190 functions (95%) have complexity≤ 12, and 10 functions (5%) exceed it.
Interpreting Values
| P95 Score | Assessment | Action |
|---|---|---|
| ≤ 10 | Excellent — even your most complex functions are manageable | Maintain current practices |
| 11 - 20 | Good — some complex functions exist but are within bounds | Watch the trend; refactor if increasing |
| 21 - 35 | Concerning — top 5% of functions are hard to maintain | Prioritize decomposing the top-5% functions |
| > 35 | Problematic — widespread high complexity | Systematic refactoring needed; set team complexity budgets |
P95 vs Max in Practice
Consider a repository with 500 functions. Here is why P95 is more useful than max for setting thresholds:
Repository: 500 functions Function complexity distribution: - 400 functions: score 1-5 (80%) - 75 functions: score 6-15 (15%) - 20 functions: score 16-25 (4%) - 4 functions: score 26-40 (0.8%) - 1 function: score 87 (0.2%) ← auto-generated parser complexity_avg = 5.2 complexity_p95 = 18 complexity_max = 87 If you set a threshold alert on max > 30: → Alert always fires because of the generated parser → You learn nothing useful If you set a threshold alert on p95 > 20: → Alert fires only when your top 5% degrades → Actionable signal about code quality trends
Reducing P95
To lower complexity_p95, you need to reduce the complexity of your most complex functions — specifically the top 5%. The strategies are the same as for reducing individual function complexity:
- Identify the functions at or above P95 from your dashboard
- Extract sub-functions to distribute complexity
- Replace conditionals with lookup tables, strategy patterns, or polymorphism
- Flatten nesting with early returns and guard clauses
- Set a team rule — no new function should exceed the current P95 value
Related Metrics
- complexity_avg — Shows the overall average; use together with P95 for a full picture
- complexity_max — The absolute maximum; useful for finding the single worst function
- symbol_count — More symbols with lower P95 indicates good decomposition