2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-06-08 14:23:48 +02:00
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.
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-06-08 14:23:48 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2021-06-08 14:23:48 +02:00
----
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;
}
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-06-08 14:23:48 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2021-06-08 14:23:48 +02:00
----
doTheThing();
doSomethingElseEntirely();
doTheThirdThing();
----
Or
2022-02-04 17:28:24 +01:00
[source,text]
2021-06-08 14:23:48 +02:00
----
for (int i=0; i < 10; i++) {
doTheThing();
}
bool myFlag = true
while (myFlag) {
doSomethingElseEntirely();
myFlag = conditional;
}
while (myFlag) {
doTheThirdThing();
if (conditionThatCouldBeFalse) {
break;
}
}
----