org.sonar.l10n.javascript.rules.javascript.S2301.html Maven / Gradle / Ivy
Why is this an issue?
A selector parameter is a boolean
parameter that’s used to determine which of two paths to take through a method. Specifying such a
parameter may seem innocuous, particularly if it’s well named.
Unfortunately, developers calling the method won’t see the parameter name, only its value. They’ll be forced either to guess at the meaning or to
take extra time to look the method up.
This rule finds methods with a boolean
that’s used to determine which path to take through the method.
Noncompliant code example
function tempt(name: string, ofAge: boolean) {
if (ofAge) {
offerLiquor(name);
} else {
offerCandy(name);
}
}
// ...
function corrupt() {
tempt("Joe", false); // does this mean not to temp Joe?
}
Compliant solution
Instead, separate methods should be written.
function temptAdult(name: string) {
offerLiquor(name);
}
function temptChild(name: string) {
offerCandy(name);
}
// ...
function corrupt() {
age < legalAge ? temptChild("Joe") : temptAdult("Joe");
}