JavaScript: Cyclomatic Complexity computation and threshold

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 () {
+1

var PageLoad = function () {
+1
.
.
.

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?

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

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.

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 ?”

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:

Hi there, following what’s written on the post, I would like to ask if there was a solution to ignore the counts inside $(document).ready(function(){ }) and count only in the methods inside it. That applies to number of lines and Cyclomatic Complexity.
I know there is ignore rule in specific file or regex pattern in a file but no or I haven’t seen ignore rule in a specified method name.

No, there is no way to configure that.

1 Like

Thank you @Lena