Skip to content

Introduce Default Else

Sourcery refactoring id: introduce-default-else

Description:

Move default state of variable into else branch

Before:

data = ""
if condition:
    data = dictionary["message"]

After:

if condition:
    data = dictionary["message"]
else:
    data = ""

Explanation:

A common pattern when coding is to set the state of a variable based on a condition. Often we set a default state first, then might set it to something different if a condition holds. In these cases Sourcery will move the default setting into the else branch, but only if this unlocks further changes.

Back to top