Details
-
Type:
Code Smell Detection
-
Status: Active
-
Resolution: Unresolved
-
Labels:
-
Message:Provide multiple methods instead of using "xxx" to determine which action to take.
-
Default Severity:Major
-
Impact:Low
-
Likelihood:High
-
Targeted languages:C#, C, C++, Flex, JavaScript, Objective-C, PHP, Python, Swift, VB.Net, VB6
-
Covered Languages:Java
-
Remediation Function:Constant/Issue
-
Constant Cost:15min
-
Analysis Scope:Main Sources
Description
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
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
public void temptAdult(String name) { offerLiquor(name); } public void temptChild(String name) { offerCandy(name); } // ... public void corrupt() { age < legalAge ? temptChild("Joe") : temptAdult("Joe"); }
Attachments
Issue Links
- is duplicated by
-
RSPEC-2495 Flag arguments should not be used
- Closed
- is implemented by
-
CPP-1556 Rule: Methods should not contain selector arguments
-
- Closed
-