Add Support for Modified Cyclomatic Complexity
It would be nice if support was added for "Modified" cyclomatic complexity. It's the metric we prefer to use because we agree that the number of cases a switch statement contains doesn't make a significant difference in how difficult it is to understand the switch statement.
From https://www.unix.com/man-page/debian/1/pmccabe/
Modified McCabe Cyclomatic Complexity
| Traditional McCabe Cyclomatic Complexity
| | # Statements in function
| | | First line of function
| | | | # lines in function
| | | | | filename(definition line number):function
| | | | | |
5 6 11 34 27 gettoken.c(35): matchparen
Column 1 contains cyclomatic complexity calculated by adding 1 (for the function) to the occurences of for, if, while, switch, &&, ||, and
?. Unlike "normal" McCabe cyclomatic complexity, each case in a switch statement is not counted as additional complexity. This treatment
of switch statements and complexity may be more useful than the "normal" measure for judging maintenance effort and code difficulty.
Column 2 is the cyclomatic complexity calculated in the "usual" way with regard to switch statements. Specifically it is calculated as in
column 1 but counting each case rather than the switch and may be more useful than column 1 for judging testing effort.
I imagine it would be relatively easy to support both the modified and traditional complexity methods. I've also seen that there is support for custom metrics, but after reading through that I'm not seeing a way to calculate the modified complexity using that functionality.
Hi Kevin,
The metric you are looking for is defined as:
+metric(function.cyclomatic_complexity_switch_modified = function.cyclomatic_complexity - function.num_switch_cases + function.num_switch_stmts)
Keep in mind that this will not work on the current version PC-lint Plus 2.0 and will work on the upcoming beta. Since the function needed to make this metric num_switch_cases is not available in PC-lint Pus 2.0. The emails for the beta release will be sent out soon.