rspec/rules/S2301/rule.adoc

52 lines
1.1 KiB
Plaintext

== Why is this an issue?
A selector argument is a ``++boolean++`` argument 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, the maintainers of the code 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.
Instead, separate methods should be written.
This rule finds methods with a ``++boolean++`` that's used to determine which path to take through the method.
=== Noncompliant code example
[source,text]
----
public String tempt(String name, boolean ofAge) {
if (ofAge) {
offerLiquor(name);
} else {
offerCandy(name);
}
}
// ...
public void corrupt() {
tempt("Joe", false); // does this mean not to temp Joe?
}
----
=== Compliant solution
[source,text]
----
public void temptAdult(String name) {
offerLiquor(name);
}
public void temptChild(String name) {
offerCandy(name);
}
// ...
public void corrupt() {
age < legalAge ? temptChild("Joe") : temptAdult("Joe");
}
----