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

break statements should not be used except for switch cases

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
      None
    • Message:
      Refactor the code to remove this break statement.
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Covered Languages:
      C#, C, C++, Objective-C
    • Irrelevant for Languages:
      VB.Net
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      10min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      break; is an unstructured control flow statement which makes code harder to read.
      Ideally, every loop should have a single termination condition.

      Noncompliant Code Example

      for (element = list.first; element != null; element = element->next) { // First termination condition
        if (!matches(element->value)) {                                      // Second termination condition
          break; // Noncompliant
        }
      
        /* ... */
      }
      

      Compliant Solution

      // Compliant
      for (element = list.first; element != null && matches(element->value); element = element->next) {
        /* ... */
      }
      

        Attachments

          Issue Links

          1.
          C# RSPEC-3322 Language-Specification Active Unassigned

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                dinesh.bolkensteyn Dinesh Bolkensteyn (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated: