Skip to content

Switch

Sourcery refactoring id: switch

Description:

Simplify conditionals into a form more like a switch statement

Before:

if item.a == 1:
    q += 2
elif item.a == 2:
    q += 2
elif item.a == 4:
    q = 0

After:

if item.a in [1, 2]:
    q += 2
elif item.a == 4:
    q = 0

Explanation:

This refactoring examines complex conditionals where a variable is being compared to various different values, and tries to put them into the simplest form possible, eliminating duplicated blocks.

Back to top