Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-2301

Public methods should not contain selector arguments

    XMLWordPrintable

    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

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              ann.campbell.2 Ann Campbell
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: