Skip to content

Hoist If from If

Sourcery refactoring id: hoist-if-from-if

Description:

Moves if statements that match a conditional out of that conditional

Before:

if hat.quality < DESIRED_QUALITY:
    happiness -= 1
    if not hat.is_stylish() and hat.quality < DESIRED_QUALITY:
        happiness -= 1

After:

if hat.quality < DESIRED_QUALITY:
    happiness -= 1
if not hat.is_stylish() and hat.quality < DESIRED_QUALITY:
    happiness -= 1

Explanation:

Where a nested conditional repeats the conditions of the outer one, it is logically equivalent to reduce the level of nesting as shown above. Doing this can make the meaning of the code slightly clearer.