This function has too many statements. (41)

21,186

Solution 1

It doesn't mean poorly managed code as @pankajparkar says before, it could be because you have something like this, lets say this from one of my projects:

  $scope.betLoader = false;
  $scope.showIfbetAlerts = true;
  $scope.displayStraight = true;
  $scope.displayParlay = true;
  $scope.displayIfBet = true;
  $scope.displayTeaser = true;
  $scope.displayPleaser = true;
  $scope.displayReverse = true;
  $scope.unavailableBet = false;
  $scope.subAccordion = false;
  $scope.betTypeShow = false;
  $scope.showStraight = true;

you can do this:

$scope.setInitialState = function() {
  $scope.betLoader = false;
  $scope.showIfbetAlerts = true;
  $scope.displayStraight = true;
  $scope.displayParlay = true;
  $scope.displayIfBet = true;
  $scope.displayTeaser = true;
  $scope.displayPleaser = true;
  $scope.displayReverse = true;
  $scope.unavailableBet = false;
  $scope.subAccordion = false;
  $scope.betTypeShow = false;
};
$scope.setInitialState();

that will fix it.

UPDATE

Let me explain:

it is not only related with the dependencies, jslint throws this error when there are too many statements, he says before on line ten which is where the controller begins, so parting from there, he should have too many statements, if you put all those statements in 1 function, those statements will be reduce to 1 :)

Solution 2

The best way to get rid of the error would be to edit your jshint settings to not display it.

http://jshint.com/docs/options/#maxstatements

That's a very wishy-washy jshint warning that doesn't really mean anything.

Normally, a function that requires more than 4 or 5 parameters is a bad idea for a lot of reasons, but isn't technically wrong. In this case those params are Angular's way of defining dependencies, so shouldn't be a problem. If the code works, I wouldn't worry about it.

Solution 3

If the controller wants more statements and you don't have any other method to remove it then go to your .jshintrc file and edit it like

"maxstatements": 80, // or whatever number you want'

thanks

Share:
21,186
Non
Author by

Non

Thus spoke the code

Updated on August 11, 2020

Comments

  • Non
    Non over 3 years

    I have this controller

      .controller('ctrl', function($scope, $rootScope, $timeout, $alert, 
                                   $location, $tooltip, $popover, BetSlipFactory, 
                                   AccordionsFactory, AuthFactory, 
                                   RiskWinCalculations) {...});
    

    and, I am getting this error due to jshint:

    line 10 col 44 This function has too many statements. (41)

    so, what should I do to avoid it ?

    • Pankaj Parkar
      Pankaj Parkar almost 9 years
      that mean your code code is poorly managed..you should make it modular it..Then your code will also having more than 1000 lines..am correct?
    • user2357112
      user2357112 almost 9 years
      Split it up into meaningful pieces with less statements? Or if the code is repetitive, make it less repetitive.
    • Pankaj Parkar
      Pankaj Parkar almost 9 years
      @NietzscheProgrammer oh thats really cool. Cant believe such case.could you remove the unused first and then check what is the number...
  • Reacting
    Reacting almost 9 years
    it is not only with dependencies, jslint throws this error when there are too many statements, he says before on line ten which is where the controller begins, so parting from there, he should have too many statements, if you put all those statements in 1 function, those statements will be reduce to 1 :)
  • Non
    Non almost 9 years
    Genius, I put all those statements together and wrapped in a function and boila :)
  • Pradep
    Pradep over 8 years
    This fixed the problem for me too. I thought jshint was worried about lots of lines of code. Apparently it is worried more about lots of assignment statements. Thanks for the answer :)
  • Andrew Craswell
    Andrew Craswell over 7 years
    How would yo solve this problem if you use controllerAs syntax rather than $scope?
  • Kandy
    Kandy almost 7 years
    I think this is the best to do since this limit controls only the readability. :)