Complexity greater than authorized in AngularJS Controller (SonarLint issue)

 ̄綄美尐妖づ 提交于 2019-12-01 21:00:19

If you want to remove complexity you can make one function :

    $scope.startgenerator = function() {
        $http.get('/start').success(function () {
            $scope.updateStatus();
        });
    };

    $scope.resumegenerator = function() {
        $http.get('/resume').success(function () {
            $scope.updateStatus();
        });
    };

    $scope.suspendgenerator = function() {
        $http.get('/suspend').success(function () {
            $scope.updateStatus();
        });
    };

    $scope.stopgenerator = function() {
        $http.get('/stop').success(function () {
            $scope.updateStatus();
        });
    };

to

$scope.generatorAction = function(action) {
    $http.get('/' + action).success(function () {
        $scope.updateStatus();
    });
};

and then use it like this:

$scope.generatorAction('stop');

Or use a service that handle your http request, It's a better practice.

Edit:

I'm using this styleguide for my angular applications : https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md

Creating a simple service for your http request :

(function() {
  'use strict';

  angular
    .module('yourModuleName')
    .factory('generator', generatorFactory);

  function generatorFactory($http) {

     var service = {
        start: start,
        resume: resume,
        suspend: suspend,
        stop: stop
     }

     return service;

     function start() {
        return $http.get('/start');
     }

     function resume() {
        return $http.get('/start');
     }

     function suspend() {
        return $http.get('/suspend');
     }

     function stop() {
        return $http.get('/stop');
     }
  }

})();

And then in your controller:

app.controller('LauncherCtrl', function ($scope, generator, $http) {

    $scope.genStatus = "stopped";

    $scope.startgenerator = function() {
        generator.start().then(function () {
            $scope.updateStatus();
        });
    };

    $scope.resumegenerator = function() {
        generator.resume().then(function () {
            $scope.updateStatus();
        });
    };

    $scope.suspendgenerator = function() {
        generator.suspend().then(function () {
            $scope.updateStatus();
        });
    };

    $scope.stopgenerator = function() {
        generator.stop().then(function () {
            $scope.updateStatus();
        });
    };

    $scope.updateStatus = function() {              
        $http.get('/status').success(function (response) {
              $scope.genStatus = response.data;
        });
    };

    $scope.updateStatus();
});

First it seems to take more code and more complexity to your app, but if you need to stop your generator in an other page or in a component/directive, you just have to inject your 'generator' service and do generator.stop(); and by doing this, if one day your endpoint url changed, you only have to change them in your service.

Is there something wrong with it?

You're looking for an objective answer to a subjective question. Let's say that the more complex a function gets, the more you (or someone else) will struggle to maintain it. This issue is telling you that you've reached an arbitrary point where the code may be getting hard to understand.

it isn't complexity 11, is it?

The way SonarQube counts complexity doesn't quite match any of the currently enunciated standards, but here's how it got the number 11:

app.controller('LauncherCtrl', function ($scope, $http) {  // +1

    $scope.genStatus = "stopped";

    $scope.startgenerator = function() {                   // +1
        $http.get('/start').success(function () {          // +1
            $scope.updateStatus();
        });
    };

    $scope.resumegenerator = function() {                 // +1
        $http.get('/resume').success(function () {        // +1
            $scope.updateStatus();
        });
    };

    $scope.suspendgenerator = function() {                // +1
        $http.get('/suspend').success(function () {       // +1
            $scope.updateStatus();
        });
    };

    $scope.stopgenerator = function() {                   // +1
        $http.get('/stop').success(function () {          // +1
            $scope.updateStatus();
        });
    };

    $scope.updateStatus = function() {                    // +1
        $http.get('/status').success(function (response) {// +1
              $scope.genStatus = response.data;
        });
    };

    $scope.updateStatus();
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!