Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript: cyclomatic complexity of the wrapper function

Most of my javascript code files look like this:

(function() {
  var Foo = function() {
    ...
  };

  var Bar = function() {
    ...
  };

  ...
}());

I've tried a number of tools that calculate the cyclomatic complexity of code, and they all generate wrong reports (from my point of view), that is: they all point their fingers at the wrapping functions as the most complex ones.

The problem with this is that all reports are badly distorted by this fact: the wrapping functions occupy often more than half of the complexity pie chart, and all average numbers are biased.

Is there a way to obtain the real complexity of my code, not biased by the wrapper functions?

Are all those tools doing it wrong? Am I doing it wrong wrapping my code inside a function for scoping (I don't think so)? Am I doing it wrong using those tools at all?

EDIT

It was suggested to remove the wrapper function before calculating complexity, and I would be happy to do it, but is there a reliable way to do it automatically?Please just ignore this and go for a proper solution.

like image 635
giorgian Avatar asked May 02 '13 10:05

giorgian


1 Answers

You can use jsmeter.herokuapp.com online or view the source at jsmeter-online which uses jsmeter by Noah Peters

I plugged in this code:

(function () {
  function testFunction(x) {
    var y;

    switch (x) {
      case 1:
        y = x;
        break;
      case 2:
        y = x * 4;
        break;
      default:
        y = 0;
        break;
    }
    return y;
  }

  var FooBar = function () {
    // ...
  };
})();

It correctly identified the internal function testFunction as higher complexity (5), and that it was wrapped in an anonymous function with complexity (1). Also works with declaring functions as var FooBar = function(){...}

Looks like the tool you're looking for.


Deprecated: Archive of jsmeter.info - looks like the current domain is spam

like image 200
Jim Bergman Avatar answered Nov 11 '22 08:11

Jim Bergman