9

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.

giorgian
  • 3,795
  • 1
  • 30
  • 48
  • Just remove the wrapper function? – Griffin May 02 '13 at 10:38
  • 3
    @Griffin what about scope? All functions and variables would be global, then. That's exactly the point of using a wrapper function. – giorgian May 02 '13 at 10:42
  • Is all your code within the wrapper function? If so, then does it matter if the variables are in global or wrapper-function scope? – Griffin May 02 '13 at 11:22
  • 1) my code is in a number of wrapper functions, each exposing a limited amount of stuff, and keeping the rest private; 2) of course it matters! You accessing my web app cannot access those variables using the inspector! – giorgian May 02 '13 at 11:37
  • I don't see why the scope matters in your complexity analysis, is what I meant. But perhaps I am missing something. – Griffin May 02 '13 at 11:41
  • I'm confused: are you suggesting to throw the wrapper away for good, or to just remove it before running the complexity analysis, and to put it back afterwards? – giorgian May 02 '13 at 13:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29313/discussion-between-giorgian-and-griffin) – giorgian May 02 '13 at 14:14
  • Removing the wrapper functions alter the side effects on your code, so I don't think yuo can do it "automatically", but if you have written `(function() {` and `}());` without parameters, it's not that hard to do a replace all in your favorite text editor. – vinczemarton May 02 '13 at 14:53
  • 1
    Would you mind telling us *which* tools you used, and what they reported? – Bergi May 04 '13 at 11:07
  • 7
    How about telling showing us a real example, the reported value, and why *you* think it is wrong? (Cyclomatic complexity is defined pretty carefully; you think the tool can't compute it right?) There isn't enough information here to understand the actual complaint. – Ira Baxter May 04 '13 at 15:25
  • 1
    Uhm, maybe i'm saying something really stupid (never run my code through such tools) but the point is that all those closures obfuscate their inner workings. Since removing all the wrappers only for the sake of analysis is crazy, you could instead pass them an optional "tracker" object and attach functions/objects you want to expose for analysis to this "tracker"? It would be available on the global scope, but would avoid pollution and bind code to be analyzed in some sort of ordered structure. My 2 cents :) – sixFingers May 10 '13 at 22:12

1 Answers1

3

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

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Jim Bergman
  • 5,207
  • 2
  • 17
  • 19