Skip to content

While-Guard-To-Condition

Documentation Index

Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt

Use this file to discover all available pages before exploring further.

Sourcery rule id: while-guard-to-condition

Section titled “Sourcery rule id: while-guard-to-condition”

Move a guard clause in a while statement’s body into its test

while (true) {
if (condition) {
break;
}
do_some_stuff();
}
while (true && !condition) {
do_some_stuff();
}

A guard clause with a break that is the first thing in a while body is equivalent to having the condition contained in the while itself.