Code Refactor of the Day - Extracting Compound Conditionals

Posted on Mon 15 January 2018 in Posts

I'm currently doing the 30-Day Code Quality Challenge (https://www.codequalitychallenge.com), and today's exercise was an interesting one -- extract a compound conditional.

The idea of extracting a compound conditional is it's a refactor to try and improve the readability of code by giving a name to a complex boolean expression. For example, say you have a block like:

def foo():
    if user.state == "active" and user.birthdate.year >= 1990 and user.birthdate.year < 2000 and user.height_in_inches > 72:
        ... do something

Now imagine we refactored it to look like this:

def foo():
    if tall_active_user_born_in_the_90s(user):
        ... do something


def tall_active_user_born_in_the_90s(user):
    return user.state == "active" and user.birthdate.year >= 1990 and user.birthdate.year < 2000 and user.height_in_inches > 72

I'd say the latter version of foo() reads much better & is much more self-documenting.