codemetrics icon indicating copy to clipboard operation
codemetrics copied to clipboard

With default config complexity and recommendations are outrageous

Open FirstWhack opened this issue 4 years ago • 3 comments

The following reports "Complexity is 6 it's time to do something..."

function thing(foo, bar) {
  if (foo) {
    return 'foo';
  } else if (bar) {
    return 'bar';
  }
}

This piece of code has an obvious cyclomatic complexity of 3.. Aside from the contrived nature of this code, it is not time to do something.

Per the readme:

Please note that it is not a standard metric, but it is a close approximation of Cyclomatic complexity.

I have another example where the actual cyclomatic complexity of a generator function (in a redux saga) is 58, but the tool reports complexity of 7. It is 200 lines of conditional yields and essentially the same complexity as the above example according to CodeMetrics. I will have to take some time to recreate it in a readable but unlicensed way later.

FirstWhack avatar Jul 08 '20 14:07 FirstWhack

I'm open for suggestions on how to improve the situation, however I think that code path could be simplified (that is the purpose of this extension, to point out such things) The reason for having the same result as the mentioned generator function is likely that yields are not counted at the moment.

I'm not sure though about what is the purpose of this issue report... yes it is not cyclomatic complexity as it is stated in the readme, yes it is imperfect, on the other hand almost every aspect of the complexity computation can be customized with a simple configuration.

You don't like that return statements add +1? Set the value associated with them to 0.

I'll keep this open so that I'll add +1 for yields as well to be consistent

kisstkondoros avatar Aug 07 '20 23:08 kisstkondoros

I think issue: Support for Cognitive Complexity #96 will go a long way to sorting out this confusion.

spartanatreyu avatar Jul 14 '21 02:07 spartanatreyu

if with else statement is 2.

but, i found an default setting 'Codemetrics › Luaconfiguration: Elseif Clause' is 1

It seems that ‘else if’ are recognized as ‘else’ and ‘if’ respectively, so it leads to 2? I think it might be more appropriate to keep it at 1

fjqingyou avatar Mar 30 '22 09:03 fjqingyou

Changed default for IfWithElseStatement to 1 available in kisstkondoros.vscode-codemetrics v1.26.1

kisstkondoros avatar Dec 28 '22 12:12 kisstkondoros