-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The ternary operator may be calculated incorrectly #19
Comments
Same here:
|
Hi @matkoch, |
I'm fine if anyone would send a pull request for this. Just keep in mind that there are many other short-hand syntaxes that should also be considered, like null coalescing, switch expressions, and compound assignments. |
I'm using Rider 2020.2.4 with CognitiveComplexity 2020.2.2.
According to Campbell's "Cognitive Complexity" paper linked on the plugin's GitHub page, shorthand (i.e.,
MyObj myObj = a?.myObj;
) should be ignored, however in the section "Increment for breaks in linear flow", the ternary operator is specifically included withif
and other conditionals, and it seems like it should generate a structural increment.The
?.
and??
shorthand operators seem like they should be ignored, but not?:
.My understanding is that the entire
?:
should be treated as a single structural increment, so when nested inside afor
loop, it should cost +2, which is less than an equivalentif-else
costs at +3 (+2 for the structuralif
and +1 for the hybridelse
). When not nested, the?:
would have a cost of +1, and theif-else
would cost +2 (+1 structural and +1 hybrid).In short, the
?:
shorthand is still encouraged over theif-else
, however it should have a complexity cost.The text was updated successfully, but these errors were encountered: