Min/Max identity
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: min-max-identity
Section titled “Sourcery refactoring id: min-max-identity”Description:
Section titled “Description:”Replaces duplicate conditionals looking for the minimum or maximum value of
multiple variables with a min or max function
Before:
Section titled “Before:”if first_hat.price < second_hat.price: cheapest_hat_price = first_hat.priceelse: cheapest_hat_price = second_hat.priceif sale_price >= 10: sale_price = 10After:
Section titled “After:”cheapest_hat_price = min(first_hat.price, second_hat.price)sale_price = min(sale_price, 10)Explanation:
Section titled “Explanation:”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.