Skip to content

Min/Max identity

Sourcery refactoring id: min-max-identity


Replaces duplicate conditionals looking for the minimum or maximum value of multiple variables with a min or max function


if first_hat.price < second_hat.price:
    cheapest_hat_price = first_hat.price
    cheapest_hat_price = second_hat.price
if sale_price >= 10:
    sale_price = 10


cheapest_hat_price = min(first_hat.price, second_hat.price)
sale_price = min(sale_price, 10)


We often need to work out the smallest or largest of two values, and the quickest way to do this in Python is to use the built-in min and max functions. This results in a shorter and clearer way to achieve the same result. The same functions also offer a shortcut for when we want to put a cap or a floor on the value of a variable.