Fork me on GitHub

JSLint Error Explanations

JSLint will hurt your feelings. It's time to make them better!

Too many parameters per function ({a})

When do I get this error?

JSHint (below version 1.0.0) will throw the "Too many parameters per function ({a})" error when it encounters a function signature with more named arguments than specified by the maxparams option. Here's an example in which we attempt to declare a function that takes 3 arguments:

Why do I get this error?

This error is raised to highlight a deviation from a coding style. The ECMAScript standard does not specify a minimum or maximum number of arguments a function can accept (ES5 §13):

FunctionDeclaration :
    function Identifier ( FormalParameterListopt ) { FunctionBody }

FormalParameterList :
    FormalParameterList , Identifier

You can see from the grammar quoted above that any number of formal parameters are valid syntax. However, you may wish to enforce a maximum number to help keep code readable and maintainable. A common strategy is to accept a single argument e.g. options, which is usually an object with various properties.

We could rewrite our Person constructor above to accept two arguments and therefore conform to the specified coding style (this is not a great example - you would probably want to check for the existence of the various settings keys and perhaps provide default values):

In JSHint 1.0.0 and above this warning has changed to "This function has too many parameters". More detail can be found the page dedicated to that message.

James Allardice

This article was written by James Allardice, an enthusiastic young JavaScript developer at Global Personals (we're looking for developers so please apply). He is passionate about writing clean, maintainable JavaScript and uses JSHint every day to help achieve this.

You can follow him on Twitter, fork him on GitHub and find him on Google+.

Proceeds generated by this site are donated to help advance other open source projects

comments powered by Disqus