Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does recursive method increase cyclomatric complexity

I do not have any programs installed for measuring cyclomatric code complexity at the moment. But I was wondering does a recursive method increases the complexity?

e.g.

// just a simple C# example to recursively find an int[]
// within a pile of string[]
private int[] extractInts(string[] s)
{
    foreach (string s1 in s)
    {
        if (s1.ints.length < 0)
        {
            extractInts(s1);
        }
        else
        {
            return ints;
        }
    }
}

Thanks.

like image 911
BeraCim Avatar asked Aug 20 '10 05:08

BeraCim


2 Answers

As far as I understand, no. There is only one linearly independent path to the recursive method in your example, so it wouldn't increase the cyclomatic complexity.

like image 127
Javid Jamae Avatar answered Oct 14 '22 23:10

Javid Jamae


  1. Loops do increase cyclomatic complexity.
  2. A loop can often be rewritten using recursion plus a guard condition.

Even if the recursive call itself would not count strictly as an increment, the guard condition does. This makes the loop and recursion+guard on par.

like image 44
ron Avatar answered Oct 14 '22 21:10

ron