Skip to content

Introduce Default Else

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: introduce-default-else

Section titled “Sourcery refactoring id: introduce-default-else”

Move default state of variable into else branch

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

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.