Calculate Cyclomatic Complexity for Javascript

JavascriptMetricsCyclomatic Complexity

Javascript Problem Overview


Are there any tools available for calculating Cyclomatic Complexity in Javascript?

I've found it a very helpful metric in the past while working on server side code, and would like to be able to use it for the client side Javascript I write.

Javascript Solutions


Solution 1 - Javascript

I helped write a tool to perform software complexity analysis on JavaScript projects:

complexity-report

It reports a bunch of different complexity metrics: lines of code, number of parameters, cyclomatic complexity, cyclomatic density, Halstead complexity measures, the maintainability index, first-order density, change cost and core size.

It is released under the MIT license and built using Node.js and the Esprima JavaScript parser. It can be installed via npm, like so:

npm i -g complexity-report

Solution 2 - Javascript

For completeness in the answers, I was looking for the same tool some time ago and didn't find anything that worked well for visualization so I wrote plato

Sample reports for :

It uses phil's complexity-report (mentioned above) and also aggregates data from jshint (and eventually, others).

Solution 3 - Javascript

Since cyclomatic complexity is evaluated counting the number of keyword "if, switch, while for break" etc.. every tools that works with C will do the job, like sourcemonitor: http://www.campwoodsw.com/sourcemonitor.html

Actually, on javascript the more you try to modulize your code, the more you will slow it down, so take it with a grain of salt ;)

EDIT: I Really can't understand what's going on on this answer, I get another downvote, when in my answer I tell a good tool for calculating cyclomatic complexity in javascript, and this in particular works very well.

For the second assertion, mine is a comment that comes from experience, I never tell don't modulize your js code, I only tell to make attention in doing it, because often there is a tradeoff with speed, and when I talk of speed I mean that 2 different slowdown can happen: at download time and at execution time (and in slow device like pda/smartphone this is important).

Since tools like this often drive developer into writing more code trying to chase the smaller index possible, but in js more code unfortunately means that slowdowns can happen, and the overuse of these tools is bad. Surelly these tools can give you hints of where your code can be improved, but you've to master how to use the tool and not blindy rely on it.

So if you downvote me again, please write a comment in which you explain why you do so, the discussion can only benefit from this, thank you and sorry for the vent.

Solution 4 - Javascript

JSHint recently added support for calculating code metrics.

You can set maximum values for:

  • maxparams - the number of formal parameters allowed
  • maxdepth - how deeply nested code blocks should be
  • maxstatements - the number of statements allowed per function
  • maxcomplexity - the maximum cyclomatic complexity
Examples

Maximum number of formal parameters allowed per function

/*jshint maxparams:3 */

function login(request, onSuccess) {
  // ...
}

// JSHint: Too many parameters per function (4).
function logout(request, isManual, whereAmI, onSuccess) {
  // ...
}

Maximum number of nested code blocks allowed per function

/*jshint maxdepth:2 */

function main(meaning) {
  var day = true;

  if (meaning === 42) {
    while (day) {
  	  shuffle();

      if (tired) { // JSHint: Blocks are nested too deeply (3).
  		  sleep();
      }
    }
  }
}

Maximum number of statements allowed per function

/*jshint maxstatements:4 */

function main() {
  var i = 0;
  var j = 0;

  // Function declarations count as one statement. Their bodies
  // don't get taken into account for the outer function.
  function inner() {
    var i2 = 1;
    var j2 = 1;

    return i2 + j2;
  }

  j = i + j;
  return j; // JSHint: Too many statements per function. (5)
}

Solution 5 - Javascript

The new version of http://jshint.com is out and has a very good cyclomatic complexity calculator

Solution 6 - Javascript

You can use the ccm tool from ARCHIVE of blunck.info or the github repo jonasblunck/ccm

It supports JavaScript, C/C++ and C#. It's free, runs on Windows (can be run on Linux and Mac OS X as well - using the Mono framework).

Solution 7 - Javascript

There's now also Yardstick: https://github.com/calmh/yardstick

It tries to calculate cyclomatic complexity for idiomatic Javascript, handling more cases than for example jscheckstyle.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKarlView Question on Stackoverflow
Solution 1 - JavascriptPhil BoothView Answer on Stackoverflow
Solution 2 - JavascriptjsoversonView Answer on Stackoverflow
Solution 3 - JavascriptkentaromiuraView Answer on Stackoverflow
Solution 4 - JavascriptSavoryBytesView Answer on Stackoverflow
Solution 5 - JavascriptautomaticAllDramaticView Answer on Stackoverflow
Solution 6 - JavascriptArneView Answer on Stackoverflow
Solution 7 - JavascriptJakob BorgView Answer on Stackoverflow