61 lines
913 B
Plaintext
61 lines
913 B
Plaintext
== Why is this an issue?
|
|
|
|
There's no point in having a loop that only executes once. Either the loop structure should be removed, or its body should be corrected not to unconditionally exit after the first iteration.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,text]
|
|
----
|
|
for (int i=0; i < 1; i++) { // Noncompliant
|
|
doTheThing();
|
|
}
|
|
|
|
bool myFlag = true
|
|
while (myFlag) { // Noncompliant
|
|
doSomethingElseEntirely();
|
|
myFlag = false;
|
|
}
|
|
|
|
while (myFlag) { // Noncompliant
|
|
doTheThirdThing();
|
|
if (alwaysTrueCondition) {
|
|
break;
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,text]
|
|
----
|
|
doTheThing();
|
|
|
|
doSomethingElseEntirely();
|
|
|
|
doTheThirdThing();
|
|
----
|
|
Or
|
|
|
|
[source,text]
|
|
----
|
|
for (int i=0; i < 10; i++) {
|
|
doTheThing();
|
|
}
|
|
|
|
bool myFlag = true
|
|
while (myFlag) {
|
|
doSomethingElseEntirely();
|
|
myFlag = conditional;
|
|
}
|
|
|
|
while (myFlag) {
|
|
doTheThirdThing();
|
|
if (conditionThatCouldBeFalse) {
|
|
break;
|
|
}
|
|
}
|
|
----
|
|
|