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

A conditionally executed single line should be denoted by indentation

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Message:
      Use indentation to denote the code conditionally executed by this "if".
    • Highlighting:
      • Primary: if (condition)
      • Secondary: immediately subsequent lines of code until the next empty line
    • Default Severity:
      Critical
    • Impact:
      High
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      APEX, Flex, Kotlin, PL/I, Ruby, Rust, Scala, Solidity, TypeScript
    • Covered Languages:
      C#, C, C++, Java, JavaScript, Objective-C, PHP, T-SQL
    • Irrelevant for Languages:
      ABAP, Cobol, Go, HTML, PL/SQL, Python, RPG, Swift, VB.Net, VB6, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      10min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • Common Rule:
      Yes

      Description

      In the absence of enclosing curly braces, the line immediately after a conditional is the one that is conditionally executed. By both convention and good practice, such lines are indented. In the absence of both curly braces and indentation the intent of the original programmer is entirely unclear and perhaps not actually what is executed. Additionally, such code is highly likely to be confusing to maintainers.

      Noncompliant Code Example

      if (condition)  // Noncompliant
      doTheThing();
      
      doTheOtherThing();
      somethingElseEntirely();
      
      foo();
      

      Compliant Solution

      if (condition)
        doTheThing();
      
      doTheOtherThing();
      somethingElseEntirely();
      
      foo();
      

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated: