1

i have some problems with complexity in my javascript. It looks like:

 function mainFunction(scope, element) {
        var eventHandlerMap = {
            'firstEvent': firstEventHandler,
            'secondEvent': secondEventHandler,
            .....
        };

        function firstEventHandler(element) {
            if (element) {
                //some code
            } else {
                //some code
            }
        }
        function secondEventHandler(element) {
            //some code
        }
        //and other EventHandler functions
}

Now mainFunction has complexity of 41, but my sonar allows no more than 10. I dont good at javascript and dont have ideas how fix it.

2 Answers2

2

The way complexity is computed should be changed by next plugin version (see https://jira.sonarsource.com/browse/SONARJS-729).

Elena Vilchik
  • 1,085
  • 6
  • 11
0

Why not move firstEventHandler and secondEventHandler outside mainFunction?

If you're trying to achieve information hiding, you could use ES6 classes or use the module pattern instead of nesting the functions.

jeznag
  • 4,183
  • 7
  • 35
  • 53