Skip to content

Hoist If from If

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.

Moves if statements that match a conditional out of that conditional

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

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.