Skip to content

While Guard To Condition

Sourcery refactoring id: while-guard-to-condition

Description

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

Before

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

After

location = "Work"
destination = "Home"
while location != destination:
    location = travel()

Explanation

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

Back to top