56 lines
843 B
Plaintext
56 lines
843 B
Plaintext
![]() |
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
|
||
|
|
||
|
----
|
||
|
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
|
||
|
|
||
|
----
|
||
|
doTheThing();
|
||
|
|
||
|
doSomethingElseEntirely();
|
||
|
|
||
|
doTheThirdThing();
|
||
|
----
|
||
|
Or
|
||
|
|
||
|
----
|
||
|
for (int i=0; i < 10; i++) {
|
||
|
doTheThing();
|
||
|
}
|
||
|
|
||
|
bool myFlag = true
|
||
|
while (myFlag) {
|
||
|
doSomethingElseEntirely();
|
||
|
myFlag = conditional;
|
||
|
}
|
||
|
|
||
|
while (myFlag) {
|
||
|
doTheThirdThing();
|
||
|
if (conditionThatCouldBeFalse) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
----
|
||
|
|