Skip to content

Lift return into if

Sourcery refactoring id: lift-return-into-if

Description:

Lift return into if

Before:

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

After:

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

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.

Back to top