
Johnny B. Getgoode tweeted a screenshot from his developer Discourse server on February 13, 2022. Experienced programmers cringed at the repeating lines of if else to determine if a number is even or odd. That prompted a lively discussion on how to “un-cringe” this function.
Here are four ways to refactor the function for readability and speed in Python: match and case, recursive inner function, modulo operator, and bitwise operator.
Read the rest of the essay on Medium. If you’re interested in supporting my writing on Medium, please follow me and become a paid member (I’ll get a referral bonus).