JavaScript: Cyclomatic Complexity computation and threshold


(Vish S.) #1

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension): sonarqube-6.1
  • what are you trying to achieve: Understand the issues reported by javascript rule
  • what have you tried so far to achieve this: Research on Cyclomatic complexity

Hi ,
While looking in to the issues reported by the javascript rule :Functions should not be too complex.
we found that its adding 1 to the complexity rating for each word “function” in the javascript file.
for example :
$(document).ready(function () {

var PageLoad = function () {

So , for each keyword “function” it seems to be adding +1 to the complexity . And also if any any sub-function has less than 10 conditions (if statement of binary operation conditions) its not reporting issue at that function inside the same javascript file but it these conditions are getting added in to the total of main function.

Could you please help us to understand if its reporting the issue correctly?

Update 8-27-2018: We found that this rule is also reporting the an issue for the JQUERY standard libraries as well in similar way. Also, Bootstrap and Knockout libraries also found the complexity reported greater than 10.
So 10 is good value to measure the cyclomatic complexity?

(Vish S.) #2

Can someone please help with this java script rule:Functions should not be too complex?

(Nicolas Bontoux) #3

Hey there,

Generally speaking the Metric Definitions documentation is a good reference for any metric-related question. And to your topic here, you’ll even see that there are dedicated insights about complexity computation (per language):
Metrics - Complexity . It seems to me that the latter covers the information you’re seeking.

(Vish S.) #4

Thanks Nicolas for the response. Could you please help me to get the answer of below question.
“Even JQUERY, Bootstrap an Knockout JavaScript libraries has the complexity reported > 10 , so this threshold of 10 seems to be good value for the measurement of complexity ?”

(Nicolas Bontoux) #5

Well before anything I need to point out that the recommendation is usually to exclude such libraries from the analysis, as those are dependencies which are not maintained by the dev team from the main project.

As for the figure of 10 as a threshold, if it’s about Cyclomatic Complexity then it’s worth reminding that this is a concept that pre-dates SonarQube. See here:

At SonarSource we’re also offering a new approach to this: Cognitive Complexity ! See this blog post, and also below discussion: