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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With