Un-Cringing An is_even() Function in Python

Johnny B. Getgoode tweeted this screenshot of if-else statements.

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).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.