docs: clarify C901 examples have same McCabe complexity#25137
Closed
algojogacor wants to merge 1 commit into
Closed
docs: clarify C901 examples have same McCabe complexity#25137algojogacor wants to merge 1 commit into
algojogacor wants to merge 1 commit into
Conversation
The 'bad' and 'good' examples in the C901 (complex-structure) documentation both have the same McCabe complexity of 4, which could be confusing since the 'Use instead' section implies the refactored version reduces complexity. Add a note explaining that the refactored version improves readability through early returns and a flatter structure, and that to meaningfully reduce McCabe complexity, complex logic should be extracted into smaller helper functions. Closes #25028
Member
|
Thanks, but we should use an example that reduces complexity or remove the "use instead" alltogether. |
ntBre
added a commit
that referenced
this pull request
May 21, 2026
The C901 docstring currently pairs a 3-deep `if/else` example with a guard-clause rewrite under "Use instead". Both versions have McCabe complexity 4, so the rewrite trips (or passes) C901 identically and the docs end up teaching the false equivalence "guard clauses lower McCabe complexity." A reader filed #25028 to flag this. In that thread, MichaReiser said the docs should either use an example that actually reduces complexity or drop the "Use instead" block altogether. A prior attempt (#25137) was closed with the same comment, and a follow-up trying a lookup-table example (#25186) was also closed, which makes me think the safer option is the second one MichaReiser offered. I dropped the "Use instead" block and added a short paragraph naming the techniques that genuinely reduce McCabe complexity (extracting helpers, lookup tables, lower branching factor), with an explicit note that mechanical guard-clause inversion does not. Docstring-only. `cargo dev generate-all` and `uvx prek run --from-ref main` both pass. closes #25028 --------- Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
thejchap
pushed a commit
to thejchap/ruff
that referenced
this pull request
May 23, 2026
The C901 docstring currently pairs a 3-deep `if/else` example with a guard-clause rewrite under "Use instead". Both versions have McCabe complexity 4, so the rewrite trips (or passes) C901 identically and the docs end up teaching the false equivalence "guard clauses lower McCabe complexity." A reader filed astral-sh#25028 to flag this. In that thread, MichaReiser said the docs should either use an example that actually reduces complexity or drop the "Use instead" block altogether. A prior attempt (astral-sh#25137) was closed with the same comment, and a follow-up trying a lookup-table example (astral-sh#25186) was also closed, which makes me think the safer option is the second one MichaReiser offered. I dropped the "Use instead" block and added a short paragraph naming the techniques that genuinely reduce McCabe complexity (extracting helpers, lookup tables, lower branching factor), with an explicit note that mechanical guard-clause inversion does not. Docstring-only. `cargo dev generate-all` and `uvx prek run --from-ref main` both pass. closes astral-sh#25028 --------- Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
anishgirianish
pushed a commit
to anishgirianish/ruff
that referenced
this pull request
May 28, 2026
The C901 docstring currently pairs a 3-deep `if/else` example with a guard-clause rewrite under "Use instead". Both versions have McCabe complexity 4, so the rewrite trips (or passes) C901 identically and the docs end up teaching the false equivalence "guard clauses lower McCabe complexity." A reader filed astral-sh#25028 to flag this. In that thread, MichaReiser said the docs should either use an example that actually reduces complexity or drop the "Use instead" block altogether. A prior attempt (astral-sh#25137) was closed with the same comment, and a follow-up trying a lookup-table example (astral-sh#25186) was also closed, which makes me think the safer option is the second one MichaReiser offered. I dropped the "Use instead" block and added a short paragraph naming the techniques that genuinely reduce McCabe complexity (extracting helpers, lookup tables, lower branching factor), with an explicit note that mechanical guard-clause inversion does not. Docstring-only. `cargo dev generate-all` and `uvx prek run --from-ref main` both pass. closes astral-sh#25028 --------- Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Clarify that the C901 (complex-structure) 'bad' and 'good' documentation examples have the same McCabe complexity.
Root Cause
The current documentation shows two examples — a 'bad' nested-if version and a 'Use instead' early-return version — but both have 4 decision points, giving them the same McCabe complexity of 4. This is confusing because the 'Use instead' section implies complexity reduction when the real improvement is readability.
Fix
Add a note explaining that:
Changes
crates/ruff_linter/src/rules/mccabe/rules/function_is_too_complex.rs: Added clarifying note to the C901 rule documentationTesting
Closes #25028