0

This probably applies to more than C and Java, of course, but those are the two languages I am more familiar with, so let us take this simple example:

int foo(int arg)
{
    if (arg > 0)
        return 1;
    return 0;
}

Now, I have seen two interpretations as far as return is concerned:

  • this is a "different code path" since it exits the method; as such any return statement should increase the complexity by 1. In this case, the sample code above has a complexity of 3; or
  • return just exits the method, no reason to count it in. In this case, the sample code above has a complexity of 1.

Is any interpretation above the "correct", canonical one as far as cyclomatic complexity is concerned? I tend to favor the latter case but I'm no CS theorist...

fge
  • 119,121
  • 33
  • 254
  • 329

2 Answers2

3

That code has two distinct paths through the function: one that corresponds to arg > 0 being true and the other corresponding to it being false.

The number of return statements involved does not affect that, since it does not change the number of distinct paths through the function. Your code could be rewritten as

int foo(int arg)
{
     int retval = 0;
     if (arg > 0) retval = 1;
     return retval;
}

which has exactly the same number of paths, despite having one less return statement.

Peter
  • 35,646
  • 4
  • 32
  • 74
  • Not to mention that you could also have written that code with the `?:` ternary operator.. Hmm – fge Jul 29 '15 at 11:57
  • 1
    The ternary operator has two distinct paths through it,so changes nothing in this case. – Peter Jul 29 '15 at 12:07
1

Your cyclomatic complexity would be 2, here's why:

Your code is essentially this:

int foo(int arg)
    {
        int out;
        if (arg > 0)
            out = 1;
        else
            out = 0;
        return out;
    }

plain return statement dont count toward complexity. You only have 1 if statement, no if else statements. Hence, your cyclomatic complexity will be 2, as there is 1 alternate path to the main path. If return statements would count, your cyclomatic complexity of the same method could be 3 OR 4 (see my code vs yours), this should make clear why return statements aren't included.

Roel Strolenberg
  • 2,922
  • 1
  • 15
  • 29
  • 1
    I believe the cyclomatic complexity of OP's code is 2, as there are clearly 2 linearly independent paths through the code. – d125q Jul 29 '15 at 11:57
  • @d125q but then there is always a path in the code, right? Does it really count in complexity calculations? Would that mean that only functions/methods _with no statements_ would have a complexity of 0? – fge Jul 29 '15 at 11:59
  • You are correct, as there is always at least 1 path, adding an if statement will make the complexity 2: https://en.wikipedia.org/wiki/Cyclomatic_complexity – Roel Strolenberg Jul 29 '15 at 12:04
  • @fge: Going by the definition of cyclomatic complexity and control flow graphs, the cyclomatic complexity of an empty function is 1. – d125q Jul 29 '15 at 12:05
  • @d125q makes sense... I need to re-read everything from the start again, I believe – fge Jul 29 '15 at 12:28