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

Increment (++) and decrement (--) operators should not be used in a method call or mixed with other operators in an expression

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Extract this [increment|decrement] operation into a dedicated statement.
    • Highlighting:
      • Primary: operator
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Legacy Key:
      IncAndDecMixedWithOtherOperators
    • Targeted languages:
      Flex
    • Covered Languages:
      C#, C, C++, Java, JavaScript, Objective-C, PHP, Swift, TypeScript
    • Irrelevant for Languages:
      ABAP, Cobol, Go, HTML, PL/I, PL/SQL, Python, RPG, T-SQL, VB.Net, VB6, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • Common Rule:
      Yes
    • CERT:
      EXP30-C., EXP50-CPP., EXP05-J.
    • PC-Lint:
      9049, 9163
    • PMD:
      AvoidMultipleUnaryOperators
    • TSLint-SonarTS:
      no-nested-incdec

      Description

      The use of increment and decrement operators in method calls or in combination with other arithmetic operators is not recommended, because:

      • It can significantly impair the readability of the code.
      • It introduces additional side effects into a statement, with the potential for undefined behavior.
      • It is safer to use these operators in isolation from any other arithmetic operators.

      Noncompliant Code Example

      u8a = ++u8b + u8c--; 
      foo = bar++ / 4;
      

      Compliant Solution

      The following sequence is clearer and therefore safer:

      ++u8b;    
      u8a = u8b + u8c; 
      u8c--; 
      foo = bar / 4;
      bar++;
      

      See

      • CERT, EXP30-C. - Do not depend on the order of evaluation for side effects
      • CERT, EXP50-CPP. - Do not depend on the order of evaluation for side effects
      • CERT, EXP05-J. - Do not follow a write by a subsequent write or read of the same object within an expression

        Attachments

          Issue Links

          1.
          Swift RSPEC-2632 Language-Specification Active Unassigned
          2.
          PHP RSPEC-3212 Language-Specification Active Unassigned
          3.
          C-Family RSPEC-3678 Language-Specification Active Unassigned
          4.
          JavaScript RSPEC-3978 Language-Specification Active Unassigned

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                Anonymous
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated: