function-too-many-parameters

Try in Playground
javascript-best-practices

oscar143

Best PracticeInformational

0

No tags

No CWE or CVE

Functions should not have too many parameters

A long parameter list can indicate that a new structure should be created to wrap the numerous parameters or that the function is doing too many things.

Resources

Sonar rules - JavaScript static code analysis

Ast Rule: function definition


function-too-many-parameters

How to write a rule
function visit(node, filename, code) {
  if (node.astType === "functiondefinition") {
    if (node.parameters.values.length > 4) {
      addError(buildError(
        node.start.line,
        node.start.col,
        node.end.line,
        node.end.col,
        `A long parameter list can indicate that a new structure should be created to wrap the numerous parameters or that the function is doing too many things.`,
        `INFORMATIONAL`,
        `BEST_PRACTICES`,
      ));
    }
  }
}

good.js

Expected test result: no error

function myFunc(param1, param2, param3, param4) {
	return;
}

bad.js

Expected test result: has error

function myFunc(param1, param2, param3, param4, param5) {
	return;
}
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
Legal
  • Security
  • Privacy Policy
  • Code Privacy
  • Terms of Service
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.