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
-
return statements followed by a local function declaration are not considered redundant, as they help with readability.