Skip to content

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”

Lift return into if

def f():
if condition:
val = 42
else:
val = 0
return val
def f():
if condition:
return 42
else:
return 0

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.