Skip to content

Score if/else chains, nested functions and commented lines consistently - #8

Merged
ethanzhoucool merged 2 commits into
mainfrom
claude/fix-complexity-metrics
Nov 3, 2026
Merged

ethanzhoucool merged 2 commits into
mainfrom
claude/fix-complexity-metrics

Conversation

@ethanzhoucool

Copy link
Copy Markdown
Owner

Summary

The README says the metrics run on the IR "so a Java method and a Python function are scored by exactly the same rules". For if/else chains that wasn't true. There were also two smaller counting bugs. All three affect the UI's stat row (cyclomatic / cognitive / max nesting / maintainability) and the findings built from those numbers.

1. else / else if chains. Tree-sitter nests else and else if under the if they continue, but the metrics treated each arm as one level deeper. Python puts the arms beside the if. Java has no else_clause, so it had no ELSE node at all: else was never counted, and never explained in the walkthrough. Measured on main, as (cyclomatic, cognitive, max nesting):

shape Python JavaScript Java C++ SonarSource / after
for containing if/else 3, 5, 2 3, 6, 3 3, 3, 2 3, 6, 3 3, 4, 2
if / elif with a nested if 4, 3, 2 4, 10, 4 4, 4, 3 4, 10, 4 4, 4, 2
five-arm else if chain 5, 5, 1 5, 36, 8 5, 4, 4 5, 36, 8 5, 5, 1
else { ...; if (c) {...} } 3, 4, 2 3, 6, 3 3, 3, 2 3, 6, 3 3, 4, 2

On main, a plain five-arm JS chain trips both the deep-nesting finding (threshold 3) and the high-cognitive-load finding (threshold 15).

2. Nested functions leaked into the outer function. cyclomatic_of said "nested functions are scored separately", but the continue only skipped the nested node itself and walk() still went through its body. An outer function whose only content is a nested helper with one if scored 2 instead of 1. max_nesting_of shared one maximum across the boundary, so a nested helper's deep loops raised a deep-nesting finding on the outer function too.

3. Lines with a trailing comment counted as comment-only. x = 1 # one was counted as a comment line and not as code, so "x = 1 # one\ny = 2 # two\n" had 0 lines of code. That number feeds the summary's SLOC and the maintainability index. Blank lines inside a block comment were also subtracted twice.

Changes

  • adapters/treesitter.py
    • _meta_branch now sets is_elif on the if that is an else branch, matching what the Python adapter means by it. That's an if inside an else_clause (JS, C++) or in Java's alternative field. Before, it was set on the outer if, and only in Java. Nothing besides the metrics reads is_elif. It also sets a new continues_parent flag, which tells the metrics the chain is nested rather than laid out side by side.
    • An else_clause that only wraps an if becomes transparent, so else if is one step of the chain in every language.
    • Java's non-if alternative is wrapped in a synthesized ELSE node, spanning the else keyword. Java's } else { lines now get the same walkthrough line as JS, C++ and Python ("Runs only when the condition above was false.").
  • analysis/metrics.py
    • Cognitive complexity: else / else if add +1 with no nesting penalty, and their bodies sit one level inside the chain.
    • Max nesting: an else / else if is at the same depth as the if it continues.
    • For a function, cyclomatic_of and max_nesting_of skip nested callables. The file-level totals still count everything, as before.
    • code_lines subtracts only lines that are entirely comments; comment_lines is unchanged.

Testing

  • pytest on Python 3.13, 3.12 and 3.11: 168 passed, 3 skipped. ruff is clean, and mypy reports the same 26 errors as before.
  • New tests, 19 of which fail on main:
    • test_if_else_chains_score_the_same_in_every_language: the 4 shapes above in all 4 languages, each asserting the full triple.
    • test_nested_functions_are_scored_on_their_own: it also checks that the file total still counts every decision.
    • test_lines_with_trailing_comments_are_still_code: Python, JS with a block comment containing a blank line, and C++ with inline /* */.
    • test_java_else_gets_explained.
  • The existing tests are unchanged and still pass, including the README's "three nested ifs score 6, three sequential score 3".
  • Bundled web samples: the walkthrough text is unchanged. The C++ sample goes from 37 to 38 SLOC (one line with a trailing comment), and its MI moves by 0.3.

Notes

  • Python still can't tell else: with only an if inside it from elif, because both produce the same AST. Both are scored as elif.
  • TRY stays in NESTING_KINDS, so a try block adds a level. That rule is the same in every language, so I left it alone.

Generated by Claude Code

claude and others added 2 commits November 2, 2026 07:06
Three metric bugs, all visible in the UI's stat row and findings:

- Tree-sitter hangs `else` and `else if` under the `if` they continue,
  so every arm counted as a deeper level. A five-arm `else if` chain
  scored cognitive 36 and nesting 8 in JavaScript and C++ (tripping the
  deep-nesting and cognitive-load findings) against 5 and 1 for the same
  Python. Java had no ELSE node at all, so `else` was never counted or
  explained. Python's `else` took a nesting penalty and an `elif` body
  was not nested. All four languages now give SonarSource's numbers:
  `else`/`else if` add one with no nesting penalty, and their bodies
  are nested one level.
- A function's cyclomatic complexity and max nesting included functions
  nested inside it, despite the comment saying they are scored
  separately: the walk skipped the nested node but not its body.
- A line with code and a trailing comment was counted only as a comment
  line, so `x = 1  # one` had zero lines of code, which also fed the
  maintainability index.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UigC7feaY8NkAy9oay3GDW
@ethanzhoucool
ethanzhoucool marked this pull request as ready for review November 3, 2026 17:18
@ethanzhoucool
ethanzhoucool merged commit a1b4b23 into main Nov 3, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants