All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.javascript.rules.javascript.UnusedFunctionArgument.html Maven / Gradle / Ivy

There is a newer version: 2.5
Show newest version

Unused parameters are misleading. Whatever the value passed to such parameters is, the behavior will be the same.

Noncompliant Code Example

function doSomething(a, b) { // "a" is unused
  return compute(b);
}

Compliant Solution

function doSomething(b) {
  return compute(b);
}

Exceptions

When writing some function callbacks some arguments might be not re-used whereas they must be part of the function signature.
For instance JQuery has the 'each' helper to iterate over arrays or objects but using the counter 'i' should remain optional:

$(["first", "last"]).each(function (i, value) {
  computeSomethingWithValue(value);
});

So the rule is the following one for function-expression: only unused arguments located after some used arguments and not followed by any other used arguments should be reported as unused. Indeed, in such case it's possible to remove them from the function signature so if there are useless they must be dropped.

Examples :

var myFirsCallBackFunction = function (p1, p2, p3, p4) {  //unused p2 is not reported but p4 is
                                              return p1 + p3; }

var mySecondCallBackFunction = function (p1, p2, p3, p4) {  //unused p1, p2 and p3 are not reported
                                              return p4; }

var myThirdCallBackFunction = function (p1, p2, p3, p4) {  //unused p1 is not reported but p3 and p4 are
                                              return p2; }

Moreover when using the 'arguments' in a function body, no parameter is reported as unused.

function doSomething(a, b, c) {
  compute(arguments);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy