Swap-If-Expression
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 rule id: swap-if-expression
Section titled “Sourcery rule id: swap-if-expression”Description
Section titled “Description”Swap if/else branches of if expression to remove negation
Before
Section titled “Before”my_hat = hats.bowler if not weather.is_sunny() else hats.baseball_capmy_hat = hats.baseball_cap if weather.is_sunny() else hats.bowlerExplanation
Section titled “Explanation”Negated conditions are more difficult to read than positive ones, so it is best
to avoid them where we can. By swapping the if and else conditions around we
can invert the condition and make it positive.