Lift Return into 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: lift-return-into-if
Section titled “Sourcery refactoring id: lift-return-into-if”Description:
Section titled “Description:”Lift return into if
Before:
Section titled “Before:”def f(): if condition: val = 42 else: val = 0 return valAfter:
Section titled “After:”def f(): if condition: return 42 else: return 0Explanation:
Section titled “Explanation:”This is a quick way to streamline code slightly. Where a value is set on each branch of an if and then immediately returned, instead return it directly from each branch.
This has removed an unnecessary intermediate variable which we had to mentally track.