I found the following paragraph regarding cyclomatic complexity on Wikipedia:
It can be shown that the cyclomatic complexity of any structured program with only one entrance 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.
That would imply a cyclomatic complexity of 3 for two arbitrary nested if statements:
if (a)
{
if (b)
{
foo();
}
else
{
bar();
}
}
else
{
baz();
}
Since exactly one of the three functions is going to be called, my gut agrees with 3.
However, two arbitrary if statements could also be written in sequence instead of nesting them:
if (a)
{
foo();
}
else
{
bar();
}
if (b)
{
baz();
}
else
{
qux();
}
Now there are four paths through the code:
- foo, baz
- foo, qux
- bar, baz
- bar, qux
Shouldn't the cyclomatic complexity of this fragment hence be 4 instead of 3?
Am I misunderstanding the quoted paragraph?