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

The ternary operator should not be used

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Convert this usage of the ternary operator to an "if"/"else" structure.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Legacy Key:
      ConditionalOperator
    • Covered Languages:
      C, C++, Java, JavaScript, Objective-C
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources, Test Sources
    • Checkstyle:
      AvoidInlineConditionals

      Description

      While the ternary operator is pleasingly compact, its use can make code more difficult to read. It should therefore be avoided in favor of the more verbose if/else structure.

      Noncompliant Code Example

      printf("%s", (i > 10 ? "yes" : "no"));
      

      Compliant Solution

      if (i > 10) {
        printf("yes");
      } else {
        printf("no");
      }
      

        Attachments

          Issue Links

          1.
          JavaScript RSPEC-1954 Language-Specification Active Unassigned
          2.
          Java RSPEC-2106 Language-Specification Active Unassigned

            Activity

              People

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

                Dates

                • Created:
                  Updated: