Skip to content

docs: clarify C901 examples have same McCabe complexity#25137

Closed
algojogacor wants to merge 1 commit into
astral-sh:mainfrom
algojogacor:fix/c901-docs-clarify-complexity
Closed

docs: clarify C901 examples have same McCabe complexity#25137
algojogacor wants to merge 1 commit into
astral-sh:mainfrom
algojogacor:fix/c901-docs-clarify-complexity

Conversation

@algojogacor

Copy link
Copy Markdown

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:

  • The refactored version may have the same McCabe complexity due to identical decision points
  • The improvement is in readability through early returns and a flatter structure
  • To meaningfully reduce McCabe complexity, extract complex logic into smaller helper functions

Changes

  • crates/ruff_linter/src/rules/mccabe/rules/function_is_too_complex.rs: Added clarifying note to the C901 rule documentation

Testing

  • The change is documentation-only; existing tests continue to pass
  • No behavioral changes to the lint rule

Closes #25028

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
@astral-sh-bot astral-sh-bot Bot requested a review from ntBre May 13, 2026 04:44
@MichaReiser MichaReiser added the documentation Improvements or additions to documentation label May 13, 2026
@MichaReiser

Copy link
Copy Markdown
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C901 complex-structure has unclear documentation

3 participants