Skip to content

Switch

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.

Simplify conditionals into a form more like a switch statement

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

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.