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

Collapsible "if" statements should be merged

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Merge this if statement with the (enclosing|nested) one.
    • Highlighting:
      • Primary: nesting if (condition)
      • Additional: nested if (condition)
        • message: (Enclosing|Nested) "if" statement
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008 recommended
    • Legacy Key:
      CollapsibleIfStatements
    • Targeted languages:
      APEX, PL/I, RPG, Rust, Solidity, VB6
    • Covered Languages:
      ABAP, C#, C, C++, Cobol, Flex, Go, Java, JavaScript, Kotlin, Objective-C, PHP, PL/SQL, Python, Ruby, Scala, Swift, T-SQL, TypeScript, VB.Net
    • Irrelevant for Languages:
      HTML, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • Common Rule:
      Yes
    • PMD:
      CollapsibleIfStatements
    • TSLint-SonarTS:
      no-collapsible-if

      Description

      Merging collapsible if statements increases the code's readability.

      Noncompliant Code Example

      if (file != null) {
        if (file.isFile() || file.isDirectory()) {
          /* ... */
        }
      }
      

      Compliant Solution

      if (file != null && isFileOrDirectory(file)) { 
        /* ... */
      }
      
      private static boolean isFileOrDirectory(File file) {
        return file.isFile() || file.isDirectory();
      }
      

        Attachments

          Issue Links

          1.
          PHP RSPEC-2379 Language-Specification Active Unassigned
          2.
          ABAP RSPEC-2380 Language-Specification Active Unassigned
          3.
          PL/SQL RSPEC-2381 Language-Specification Active Unassigned
          4.
          VB6 RSPEC-2382 Language-Specification Active Unassigned
          5.
          JavaScript RSPEC-2383 Language-Specification Active Unassigned
          6.
          Swift RSPEC-2500 Language-Specification Active Unassigned
          7.
          C-Family RSPEC-2889 Language-Specification Active Unassigned
          8.
          C# RSPEC-2913 Language-Specification Active Unassigned
          9.
          Flex RSPEC-3295 Language-Specification Active Unassigned
          10.
          Cobol: Collapsible "IF" statements should be merged RSPEC-3720 Language-Specification Active Unassigned
          11.
          Python RSPEC-4006 Language-Specification Active Unassigned
          12.
          T-SQL RSPEC-4096 Language-Specification Active Unassigned
          13.
          Go RSPEC-4596 Language-Specification Active Unassigned
          14.
          Kotlin RSPEC-4683 Language-Specification Active Unassigned
          15.
          Ruby RSPEC-4755 Language-Specification Active Unassigned
          16.
          TypeScript RSPEC-4795 Language-Specification Active Unassigned
          17.
          Scala RSPEC-4909 Language-Specification Active Unassigned
          18.
          Apex RSPEC-4965 Language-Specification Active Unassigned
          19.
          VB.NET RSPEC-5220 Language-Specification Active Unassigned

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                freddy.mallet Freddy Mallet (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated: