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
C/C++ several "case:" labels together treated as separated #82
Comments
@vicgonzalez theoretically, each "case" is just an if statement and adding one branch, leaving the branch empty might reduce the possible output but doesn't reduce the possible inputs. From testing perspective, the amount of tests needed is decided by the possible inputs not the outputs. Just like an if statement with one condition will add 1 cc with or without an else statement. And by the way, if an 'if' statement's condition contains an "OR" or "AND" it will be counted as 2. Again, think of the amount of possible input instead of output. However, in reality the conditions in a switch/case statement usually have some inherent constraints and do not really add that much possible inputs (that are meaningful). So there're different opinions. Some people contributed a -m option to Lizard to "Calculate modified cyclomatic complexity number," which will count the entire switch/case as one CC. So just run:
Will do. Personally, I would not use it. Because if there is really an inherent constraint with the cases, I would use an array or a dictionary to ensure that constraint, instead of using switch/cases and leave too many possibilities. Or polymorphism, as many OO people would suggest, which in my speculation, might be a better alternative for several "cases" sharing one piece of code. |
I've done a little research and I think I'm not the first with that kind of issue: |
Hmm, are you suggesting that I should make the modified CCN as default?
|
No, what I meant is that for 'not modified' cyclomatic complexity there are people that counts each case separately (like you) and people that counts fall through cases as one, like McCabe in: |
@vicgonzalez I've made a new option "-EMcCabe" to enable the McCabe version of cyclomatic complexity. I've created a new release (1.10.2) with some other fixes. |
If several "case:" labels in a switch are together (without other statements or breaks in the middle) they are treated as separate paths increasing the complexity number.
switch(var){
case 1:
case 2:
doSomething();
break;
}
Since they are like ORs, they should be treated as one single execution path. Has to be taken into account also for the modified cyclomatic complexity.
The text was updated successfully, but these errors were encountered: