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.
Sourcery refactoring id: hoist-if-from-if
Section titled “Sourcery refactoring id: hoist-if-from-if”Description:
Section titled “Description:”Moves if statements that match a conditional out of that conditional
Before:
Section titled “Before:”if hat.quality < DESIRED_QUALITY: happiness -= 1 if not hat.is_stylish() and hat.quality < DESIRED_QUALITY: happiness -= 1After:
Section titled “After:”if hat.quality < DESIRED_QUALITY: happiness -= 1if not hat.is_stylish() and hat.quality < DESIRED_QUALITY: happiness -= 1Explanation:
Section titled “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.