Skip to main content

avoid-complex-loop-conditions

added in: 1.23.0
Pro+

Warns when a loop condition is too complex.

Keeping the loop conditions complex reduces readability. Consider moving some conditions out, or moving the entire condition into a method.

Example

❌ Bad:

void fn(int maxRotationAttempts, bool hasSuccessfulResponse) {
for (var attempts = 0;
attempts < maxRotationAttempts && !hasSuccessfulResponse; // LINT: Avoid complex loop conditions. Try moving it out to a method or check it separately.
attempts++) {}

for (var attempts = 0;
attempts < maxRotationAttempts && _condition(); // LINT: Avoid complex loop conditions. Try moving it out to a method or check it separately.
attempts++) {}
}

✅ Good:

void fn(int maxRotationAttempts, bool hasSuccessfulResponse) {
if (!hasSuccessfulResponse) {
for (var attempts = 0; attempts < maxRotationAttempts; attempts++) {
...
}
}
}