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