-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
cyclomatic complexity of ternary/null coalescing in control structures #3501
Comments
I think the new change is correct as it's more accurately counting the conditions in the IF that would evaluate it to TRUE or FALSE. Keen to hear what @MarkBaker thinks. |
There are a number of ways of writing this code:
would be another way of expressing the same code logic using an intermediate assignment; and which clearly should increase the complexity by two - once for the I believe that the complexity is still reflected accurately here - as least as accurately as Cyclomatic Complexity can be measured - in reflecting both the The CYC count isn't 100% accurate: coding structures have evolved since Henry McCabe's original 1976 paper on the subject. At the end of the day, Cyclomatic Complexity is a guideline that we can use to assess the maintenance risks of our code, not a hard and fast rule. Even the warning and error levels that we apply are just guidelines based on general principles of what is considered complex. If it's any consolation; the most complex method in my most popular OSS library has a CYC of over 21 million. I've tried refactoring it, but any refactoring has a significant adverse affect on performance. That CYC makes it a pain to maintain; but a 3x performance overhead would make it impossible to run. I choose to live with that maintenance risk for the benefit of performance. |
Okay, i can accept that argument, but still weird that some operators count and some doesn't. |
And another theoretical question: Shouldn't optional chaining counted too? |
This is probably a call for Greg to make. In McCabe's original paper, he refers to as an upper threshold of 10 as a “reasonable, but not magical, upper limit”. But a subsequent presentation that he gave on the topic ('Software Quality Metrics to Identify Risk') and published as a NIST advisory used the following table to assess risk based on complexity:
Steve McConnell in his book "Code Complete" was stricter:
More recently, Phil Koopman seems to suggest thresholds of 10-15, and 30-50 (so potentially 15 and 30) although he's thinking in terms of embedded systems Personally, I'd have no problem with setting the thresholds at 15 and 30 rather than 10 and 20; to make allowance for more modern coding structures and expressions. |
It probably should, because it's an early break from the full expression, one that I'd forgotten while I was looking at what was missing from the sniff. There's other omissions too: An expression like;
or the use of The CYC check is fairly simplistic in that it simply looks for certain tokens inside a function/method, and increments the CYC value by 1 each time it finds one of those tokens. Other possible omissions are subject to debate. In a
should increment CYC by 13 rather than by 4 (+1 for the leap year ternary) A Getting back to your actual question though, I'd say that yes, the nullsafe operator ( |
brake and continue in a loop are (almost) always inside of an if statement, so that would be double counting imo |
Normally; but you could get very dirty code like $data = [
'Hello',
'World',
'Terminate',
'Goodbye',
];
foreach ($data as $entry) {
switch ($entry) {
case 'Terminate': break 2;
default: echo $entry, PHP_EOL;
}
} |
Yes, and I can use goto and eval() too :) But I don't think there is an common part of the ones who are doing that and who are caring about code quality :D |
@gsherwood Any opinion on changing the Warning and Error thresholds for CYC? |
@MarkBaker The thresholds can be customized per project via the project custom ruleset: |
yes, I can, and I'm doing it right now in my projects. But the question here is should the recommended default values go up with the recent changes in the calculation. And I think they should. |
Describe the bug
This might not be a bug, I'm just curious of your opinion
After #3469 multiple part of my code is reporting too much cyclomatic complexity.
i have something similar:
Until now one of this statements was +1 to complexity, not it's +2 because the null coalescing. But it's still 2 options here. stepping into the if or stepping it over.
Yes I can rewrite it to
if ($variable1ThatCanBeNullOrFloat !== null && $variable1ThatCanBeNullOrFloat !== 0.0)
but it's a lot more character and I like to spare my keyboard.what do you think?
The text was updated successfully, but these errors were encountered: