While utilizing SonarLint in Eclipse, I encountered an issue while working on an AngularJS application. Specifically, I was cleaning up a controller to improve readability when SonarLint flagged the following problem:
The function has a complexity of 11, exceeding the authorized limit of 10.
Here is the code snippet for the controller in question:
app.controller('LauncherCtrl', function ($scope, $http) {
$scope.genStatus = "stopped";
$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();
});
};
$scope.updateStatus = function() {
$http.get('/status').success(function (response) {
$scope.genStatus = response.data;
});
};
$scope.updateStatus();
});
I am puzzled as to why this triggered the complexity issue. The only nested functions seem to be the start/stop/resume/pause functions calling the update function. Upon careful inspection, I verified the correct syntax with brackets and parentheses as well.