SonarQube JavaScript Cyclomatic Complexity Calculation

185 views
Skip to first unread message

chris....@wellcentive.com

unread,
Jun 29, 2016, 1:53:33 PM6/29/16
to SonarQube
Hey,

I noticed that nested functions in JavaScript increment cyclomatic complexity of the parent function by 1, but that the parent function also includes the complexity of the nested functions. I don't think this should be the case.

They do not fit the definition of cyclomatic complexity which includes only control-flow statements.

In many cases, JavaScript functions are more analogous to classes in how they are used. Especially in frameworks such as AngularJS, they are used this way. Being a dependency of the UI, most of the nested functions in these are declarations for very simple tasks like switching a boolean on/off or formatting the model to be presented a certain way. If we adhere to normal cyclomatic complexity 10, you end up with a cumbersome FizzBuzz disaster.

Please consider changing the way cyclomatic complexity is computed in JavaScript. Cyclomatic complexity is a good tool for limiting complexity, encouraging maintainability, and facilitating readability, but this particular nuance does not support these three goals and causes more problems than it solves.

Thanks,
Chris

Elena Vilchik

unread,
Jul 5, 2016, 8:42:16 AM7/5/16
to SonarQube, chris....@wellcentive.com
Hello,

Really this is a hot topic since this behaviour was introduced. I hope it will be changed in next version of plugin (ticket https://jira.sonarsource.com/browse/SONARJS-729)
Thanks for the feedback!

Elena VILCHIK | SonarSource
Language Team
Reply all
Reply to author
Forward
0 new messages