org.sonar.l10n.javascript.rules.javascript.S1536.html Maven / Gradle / Ivy
Why is this an issue?
Function parameter names should be unique in JavaScript. Unique parameter names ensure that there is no ambiguity in referring to specific
parameters within the function body. If multiple parameters share the same name, it becomes unclear which parameter is being referred to when using
that name within the function.
Unique parameter names improve the readability and maintainability of code. When parameter names are descriptive and distinct, it becomes easier
for other developers (including yourself) to understand the purpose and functionality of the function.
When parameter names are not unique, the later occurrence of a parameter will overwrite the earlier occurrence, potentially leading to unintended
consequences or bugs. This behavior can cause confusion and make the code harder to debug.
function f(a, b, a) { // Noncompliant: The first occurrence of `a` will be overwritten by the later occurrence
console.log(a, b);
}
f(1, 2, 3); // Outputs 5
In strict mode, JavaScript enforces stricter rules and detects potential issues. Duplicate parameter names are considered a syntax error in strict
mode. By using unique parameter names, you ensure compatibility with strict mode and can benefit from the enhanced error checking and code quality
improvements it provides.
'use strict';
function f(a, b, a) { // Noncompliant: SyntaxError: Duplicate parameter name not allowed in this context
console.log(a, b);
}
f(1, 2, 3);
You should remove the duplicates or rename them while carefully ensuring you are not altering the semantics of your code.
function f(a, b, c) {
console.log(a, b, c);
}
f(1, 2, 3);
Resources
Documentation
- MDN web docs - Functions
- MDN web docs - The arguments object
- MDN web docs - Strict mode