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

location = "Work"
destination = "Home"
while True:
if location == destination:
break
location = travel()
location = "Work"
destination = "Home"
while not location == destination:
location = travel()

Removing the guard clause simplifies the code and makes clearer the intention of the loop.