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.
public String feed(String name, boolean isHuman) {
if (isHuman) {
// implementation for human
} else {
// implementation for animal
}
}
// Intent is not clear at call site
feed("Max", false); // does this mean not to feed Max?
public void feedHuman(String name) {
offerSushi(name);
}
public void feedAnimal(String name) {
offerCarrot(name);
}
// Clear intent at call site
feedHuman("Joe");
feedAnimal("Max");