1

Let's suppose we have the following code:

public void testIt(boolean a, boolean b){
   if (a){
   ...
   }
   if (b){
   ....
   }
}

As I know there are three methods to calculate. I will use two of them: region formula and the rule of thumb.

Using region formula we have two regions if(a){this is the first one} and if (b) {this is the second one}. So CC = two regions +1 = 3.

The rule of thumb is from Testing for ISTQB Advanced Level Technical Test Analyst vol 3 book:

Now, this is all simple enough for a small, modest method like this. For larger functions, drawing the graph and doing the calculation from it can be really painful. So, a simple rule of thumb is this: Count the branching and looping constructs and add 1. The if statements, for, while, and do/while constructs, each count as one. For the switch/case constructs, each case block counts as one. In if and ladder if constructs, the final else does not count. For switch/case constructs, the default block does not count. This is a rule of thumb, but it usually seems to work.

So according to this rule CC = two if conditions +1 =3.

However, on stackoverflow in chat with one user I was said that the CC of this code = 4 (he used the third method) and the following image by that user was provided: enter image description here

So what is the cyclomatic complexity of this code? 3 or 4 or?

Pavel_K
  • 10,748
  • 13
  • 73
  • 186
  • can you expand usage of 3rd method? I mean according to `E − N + 2P`, we have `7 - 6 + 2*1 = 3` – Iłya Bursov Apr 12 '16 at 17:38
  • @Lashane Unfortunately I am not good with the third method but the formula you provided is like it. – Pavel_K Apr 12 '16 at 17:40
  • so, somebody gave you a picture and said "its 4", without any explanation how he/she calculated it? – Iłya Bursov Apr 12 '16 at 17:42
  • @Lashane this question I asked because the answer (http://stackoverflow.com/a/36566301/5057736) which I got for another question I can't accept as I see it is not logical, however the others upvote it. Trying to analyse I came to this new question. – Pavel_K Apr 12 '16 at 17:43
  • https://en.wikipedia.org/wiki/Cyclomatic_complexity#Implications_for_software_testing CC for this example is 3 – Iłya Bursov Apr 12 '16 at 17:47
  • @Lashane Thank you for your help. Could you then take a look at this question - http://stackoverflow.com/questions/36563667/cyclomatic-complexity-and-basis-path – Pavel_K Apr 12 '16 at 17:48
  • So, CC = 3 does not mean 3 tests, Number of paths = 4 does not mean CC is 4 – Iłya Bursov Apr 12 '16 at 18:00

1 Answers1

3

Cyclomatic complexity of this code is 3, according to definition

M = E − N + 2P,

where

E = the number of edges of the graph.
N = the number of nodes of the graph.
P = the number of connected components.

M = 7 - 6 + 1*2 = 3

In relation to software testing it means:

M is an upper bound for the number of test cases that are necessary to achieve a complete branch coverage.

M is a lower bound for the number of paths through the control flow graph (CFG).

All three of the above numbers may be equal: branch coverage <= cyclomatic complexity <= number of paths.

So, you need 2 tests to provide branch coverage. But it does not mean that it will cover all paths.

To cover all paths here you need 4 tests.

To understand it, consider following update to your code:

public String testIt(boolean a, boolean b){
    String s = "";
    if (a) s = s+"a";
    if (b) s = s+"b";
    return s;
}

to cover both branches you can test (true,true) and (false,false)

but number of possible paths (outcomes) is 4:

"", "a", "b", "ab"

which absolutely fine with formula above 2 <= 3 <= 4

Community
  • 1
  • 1
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
  • Thank you very much for your explanation! Could you provide the formula/ways how to define maximum number of paths? – Pavel_K Apr 13 '16 at 03:30
  • Am I right: for the example code you provided (with Strings) if we used `basis` path testing we any way had 3 tests? So using basis path testing we would omit one test (any one)? – Pavel_K Apr 13 '16 at 05:03
  • @JimJim2000 I'm not sure that formula exists, usually you need to write function to calculate it, but this particular case is simple 2*2 (two states of a and two states of b, both are independent), I've just counted them manually. For basis path testing theory guarantees that 3 tests will cover all branches, but it does not mean that you must have 3 tests or you can omit any test. For example tests (true,true) (true,false) will not cover all branches – Iłya Bursov Apr 13 '16 at 14:31