S0002 function-complexity¶
Rule: A function's cyclomatic complexity stays under its limit.
| Code | S0002 (stable, never reused) |
| Id | function-complexity |
| Category | size |
| Source | spirit_level |
| Fix | by hand |
Level per tier¶
| Tier | Level | Reason |
|---|---|---|
| small | warn |
more paths than can be tested or held in one's head |
| small-workspace | warn |
more paths than can be tested or held in one's head |
| medium | warn |
more paths than can be tested or held in one's head |
| big | warn |
more paths than can be tested or held in one's head |
| ai | off | |
| embedded | off | |
| ui | off | |
| egui | off | |
| units-strict | off | |
| units-paranoid | off |
Parameters¶
| Name | Type | Default | Meaning |
|---|---|---|---|
max |
integer | 15 |
the highest complexity allowed |
Why¶
Cyclomatic complexity counts the independent paths through a function: 1, plus one for each if, loop, ?, &&, || and each match arm after the first. Above about 15 a function cannot be tested path by path or held in one's head.
How to fix¶
Pull branches out into functions, turn chains of conditions into a table or an enum with methods, return early instead of nesting.
Configure¶
[lints.function-complexity]
level = "deny"
max = 15
reason = "why this project wants it at this level"
Allow one finding in the code with // spirit-level: allow(function-complexity) <reason> (waivers); the code works there too, as in [lints.S0002].