S3626 — Jump statements should not be redundant

Language
C#
Type
Code smell
Severity
Minor
Tags
redundant, clumsy

Why is this an issue?

Jump statements, such as return, yield break, goto, and continue are used to change the normal flow of execution in a program. However, redundant jump statements can make code difficult to read and maintain.

Noncompliant code example


void Foo()
{
  goto A; // Noncompliant
  A:
  while (condition1)
  {
    if (condition2)
    {
      continue; // Noncompliant
    }
    else
    {
      DoTheThing();
    }
  }
  return; // Noncompliant; this is a void method
}

Compliant solution


void Foo()
{
  while (condition1)
  {
    if (!condition2)
    {
      DoTheThing();
    }
  }
}

Exceptions

↑ Back to top