Conversation
_calculate_cyclomatic_complexity added two for any line holding a ternary, because two rules matched the same text: the statement test also fired on " if " anywhere in the line, and the ternary count then fired again for the same occurrence. The statement test now matches only a line that begins with "if ", leaving in-line occurrences to the ternary rule that already handles them. Measured on a probe project against this tree. A function whose only decision point is one ternary went from 3 to 2, which is the correct value for base 1 plus one branch. A function with if, and, elif, or, for, while and one ternary went from 9 to 8, hand counted as 8. No other decision point changes. This matters beyond the number because _check_complexity assigns critical severity above cyclomatic_critical, so the over-count can fail a CI gate for complexity a function does not have.
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.
Fixes the double count reported in #16.
_calculate_cyclomatic_complexityadded two for any line holding a ternary. The statement test fired onifappearing anywhere in the line, and the ternary count then fired again for the same occurrence. The statement test now matches only a line beginning withif.Measured on a probe project against this tree, one change at a time:
if,and,elif,or,for,while, one ternaryThe second one is 8 by hand count, so the fix removes the double count and touches no other decision point.
One case it does not address, and does not make worse: a line beginning with
ifthat also holds a ternary is still counted once, because the ternary rule already excludes such lines.Three other defects in the same function are left for separate changes:
matchcounted once whatever its arm count, keywords inside string literals read as code, and physical-line scanning that drops an operator opening a\-continued line. The last was found after #16 and is the largest; repro there.