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

Sequential tests should not check the same condition

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      This condition was just checked on line n.
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Targeted languages:
      ABAP, C, C++, Cobol, Flex, Java, Objective-C, PHP, Python, RPG, VB.Net, VB6
    • Covered Languages:
      C#, Swift
    • Irrelevant for Languages:
      JavaScript
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      When the same condition is checked twice in a row, it is either confusing - why have separate checks? - or an error - some other condition should have been checked in the second test.

      Noncompliant Code Example

      if (a == b) { // Compliant; a reassigned in previous block
        doSomething(b);
      }
      if (a == b) {  // Noncompliant; is this really what was intended?
        doTheThing(c);
      }
      

      Compliant Solution

      if (a == b) { 
        doTheThing(b);
        doTheThing(c);
      }
      

      or

      if (a == b) { 
        doTheThing(b);
      }
      if (b == c) {
        doTheThing(c);
      }
      

      Exceptions

      Since it is a common pattern to test a variable, reassign it if it fails the test, then re-test it, that pattern is ignored.

        Attachments

          Issue Links

          1.
          Swift RSPEC-2923 Language-Specification Active Unassigned
          2.
          C# RSPEC-3117 Language-Specification Active Unassigned

            Activity

              People

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

                Dates

                • Created:
                  Updated: