Open somethingSTRANGE opened 3 years ago
Same here:
FYI, I let this review from the author and she approved all tests back then. If you have any questions/concerns I'd ask you to ping her on Twitter, and only report back here when she agrees. Her account is https://twitter.com/GAnnCampbell.
Hi @matkoch,
so why don't you treat ?:
like an if
like G. Ann Campbell recommends?
My problem now is that the complexity your plugin and SonarQube calculate are different.
Can't you make a settings where the user can decide whether to count '?:' or not?
(Nevertheless I really like your plugin!)
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.