# Cyclomatic complexity with two IFs - why it is 3?

I have read an article with following example:

``````void func()
{
if (condition1)
a = a + 1;
if (condition2)
a = a - 1;
}
``````

It says the CC is 3 as there are three possible paths. How come? Why not 4? I Would expect TRUE,TRUE; FALSE,FALSE; TRUE, FALSE and FALSE, TRUE.

Does not matter what the statements are. CC=Ifs-EndPoints+2. It is always 3 for 2 IFs and one ending..

-

If your article "says the CC is 3 as there are three possible paths" then the article is missing some of the detail. The wikipedia definition of cyclomatic complexity defines it in terms of the number of nodes and edges in the graph of the function: M = E − N + 2P.

This is the graph of your function:

``````    (+)
|
(if)
|\
| (stmt)
|/
(if)
|\
| (stmt)
|/
(*)
``````

E = 7, N = 6, P = 1 so M = 7 - 6 + 2*1 = 3.

-
Thanks. On the Wiki it says about CC that " It directly measures the number of linearly independent paths through a program's source code". And I still cannot see why it is not 4. – user970696 Jan 17 '13 at 13:48
Starting with one path, you add an if, which makes two paths. You add another independent if, it adds an extra path, making 3. It's linear, not exponential, on the number of 'ifs' – Pete Kirkham Jan 17 '13 at 13:50
Now I see. But then I kinda do not get the concept of using it for testing as test cases should go through as many paths as possible and if there are 2 ifs in the function, I would say 4 must be exercised. – user970696 Jan 17 '13 at 14:04
@user970696 that is the npath complexity rather than the cyclomatic complexity. The number of test cases is: `cyclomatic <= test cases <= npath`. The cyclomatic complexity tells you the minimum number of tests to exercise all the code - not the number of tests to exercise every possible situation. – MichaelT Jan 17 '13 at 14:40
@MichaelT But what I dont understand - I could exercise all code with having all TRUE and all FALSE in just 2 runs, couldn't I? – user970696 Jan 17 '13 at 15:15

TRUE,TRUE has the same outcome as FALSE,FALSE.

``````//TRUE,TRUE
void func()
{
a = 1;

if (condition1)
a = a + 1; // a == 2
if (condition2)
a = a - 1; // a == 1

// a == 1
}

//FALSE,FALSE
void func()
{
a = 1;

if (condition1)
a = a + 1; // a == 1
if (condition2)
a = a - 1; // a == 1

// a == 1
}

//TRUE, FALSE
void func()
{
a = 1;

if (condition1)
a = a + 1; // a == 2
if (condition2)
a = a - 1; // a == 2

// a == 2
}

//FALSE, TRUE
void func()
{
a = 1;

if (condition1)
a = a + 1; // a == 1
if (condition2)
a = a - 1; // a == 0

// a = 0
}
``````
-
What do you mean? In this example yes, but different statements are run. Imagine there is a=a+5 and the other one is a=1000;. – user970696 Jan 17 '13 at 13:17
If the operations change, it is possible to have four instead of three possible outcomes. In this specific scenario, only three outcomes are possible. The reason is because the second operation is the opposite of the first one. So both happening is the same as none happening. – Kristof Claes Jan 17 '13 at 13:19
I dont think so as the formulae for CC is: Number of IFs - Number of end points + 2. For this code it makes 3 (2-1+2), regardless of what the statements are. – user970696 Jan 17 '13 at 13:22
Ah, I see. But what's the problem then? The article says it's 3, you say it has to be 3 according to the CC formula, yet you ask why it isn't 4? – Kristof Claes Jan 17 '13 at 13:26
Well, because I cannot understand why it is not 4 - doesn't it miss one path? I'm sure there is a mathematical proof why it doesn't but in the examle, I would say it does. – user970696 Jan 17 '13 at 13:30

ABSTRACT: "Researchers have often commented on the high correlation between McCabe’s Cyclomatic Complexity (CC) and lines of code (LOC). Many have believed this correlation high enough to justify adjusting CC by LOC or even substituting LOC for CC. However, from an empirical standpoint the relationship of CC to LOC is still an open one. We undertake the largest statistical study of this relationship to date. Employing modern regression techniques, we find the linearity of this relationship has been severely underestimated, so much so that CC can be said to have absolutely no explanatory power of its own. This research presents evidence that LOC and CC have a stable practically perfect linear relationship that holds across programmers, languages, code paradigms (procedural versus object-oriented), and software processes. Linear models are developed relating LOC and CC. These models are verified against over 1.2 million randomly selected source files from the SourceForge code repository. These files represent software projects from three target languages (C, C++, and Java) and a variety of programmer experience levels, software architectures, and development methodologies. The models developed are found to successfully predict roughly 90% of CC’s variance by LOC alone. This suggest not only that the linear relationship between LOC and CC is stable, but the aspects of code complexity that CC measures, such as the size of the test case space, grow linearly with source code size across languages and programming paradigms."

This is a very strong statement.

-