# 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..

## 2 Answers

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. – John V 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. – John V 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. – user40980 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? – John V 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;. – John V 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. – John V 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. – John V Jan 17 '13 at 13:30