Hi everyone,

I'm writing a blog article about McCabe Cyclomatic Complexity. I put a simple PL/SQL function in Toad that I expect should have a complexity score of 3, but Code Analysis is giving me a score of 5. You can see the code below, and I labeled each path with comments.

CREATE OR REPLACE FUNCTION TEST_MCCABE (INVAL NUMBER)

RETURN NUMBER

IS

VAR_A NUMBER;

VAR_B NUMBER;

BEGIN

VAR_A := INVAL;

```
IF VAR_A > 0
THEN
VAR_B := 1; --path 1
ELSE
IF VAR_A < 0
THEN
VAR_B := -1; --path 2
ELSE
VAR_B := 0; --path 3
END IF;
END IF;
RETURN VAR_B;
```

END;

/

Here is what I think the flow graph is:

Which is 7 shapes, 8 edges, and 1 exit point, thus leads to the calculation:

M = E - S + 2*P

M = 8 - 7 + 2 * 1

M = 3

I'm using these as references:

geeksforgeeks.org

tutorialspoint.com

Am I doing something wrong or is the Code Analysis wrong?

Thanks,

Mat

Not an expert on this stuff by any means, but only thing I can think of…

Are you sure there are not two exit points?

e.g. one for each complete If-Then-Else path?

You have two in your code, so that would yield a complexity score of 5.

Hi Gary,

Thanks for chiming in. I'm not 100% sure, but believe I only have one "exit" from the program which is the "RETURN VAR_B" statement. Unless errors or exceptions count as a exit path?

Each If-Then-Else predicate causes two paths, which are counted as Edges in the formula. Then you subtract out the number of Shapes. McCabe is only suppose to count unique pathway to program exit, which I can only count 3.

I modeled this PL/SQL example after the example the tutorialspoint.com link. They calculated as 3.

Thanks,

Mat

Yeah, it looks like your example was close to or exactly the same as the one on one of the websites.

Also, it looks like one of the sites was not counting the exit points. not sure if this means a deviation from the standard formula, or not.

However, just to be clear, one if-then-else construct does not constitute more than one exit path unless there is an embedded if then else inside, which your example has. I counted each endIF as one exit point from an IF-Then-Else path.

that said, I am not an expert on this stuff either. And it does appear that you have found something that the developers need to look into, taste on the discrepancy we see between our calculations and the website's calculations.

-G-

Hi Mat,

McCabe's Cyclomatic Complexity is designed to measure distinct code paths as described in the two links you have listed. Most people will lay out graphs like the ones in the examples and the one you posted in order to calculate it manually through visualization. When you base the calculation off the interpretation of a visual graph, it requires you to generate the flowchart in order to do the calculation, but the ultimate goal is still to calculate logical pathways. Toad, instead, parses the code to count the individual logical pathways in the code. From Toad's perspective, it sees the following in your code:

- A unique entry point and path
- A divergence defined by the first IF statement creating two distinct pathways
- A divergence in the second pathway with another IF statement that creates two additional pathways.

From that perspective, it calculates one pathway for the overall program, two pathways for the first IF statement, followed by an additional two pathways for the second statement. That produces a total of 5 local pathways. It could be argued that it could be reduced to four (or even three) based on the interpretation of the visual graph. In that sense, perhaps it could be revisited to see if it could tweaked a bit; however, the overall goal of this metric is to keep the McCabe level low (generally a value of 10 or lower). In that sense, the calculated metric by Toad I believe still provides valuable information for the overall goal.

That being said, I'll add an item to revisit this calculation to see if there are ways it can be further tweaked in the future.

Thanks for letting us know about this,

-John

I agree, it's still a helpful metric, even if it slight deviates from the formulas I found online. There's probably many ways that visualization could have been drawn. Thanks for the explanation, John.

Cyclomatic complexity - Wikipedia looks a valuable resource. Quoting:

McCabe showed that the cyclomatic complexity of any structured program with only one entry point and one exit point is equal to the number of decision points (i.e., "if" statements or conditional loops) contained in that program plus one.

Then M would equal 3.

John, why does Toad count "one pathway for the overall program" and four pathways for the IFs rather than three? Quoting again:

If the code had one single-condition IF statement, there would be two paths through the code: one where the IF statement evaluates to TRUE and another one where it evaluates to FALSE, so the complexity would be 2. Two nested single-condition IFs, or one IF with two conditions, would produce a complexity of 3.

Andre