Skip to content

Merge Else If Into Elif

Sourcery refactoring id: merge-else-if-into-elif

Description:

Merge else clause's nested if statement into elif

Before:

def interpret_response(response):
    if response.status == '200':
        return response.data
    else:
        if response.status == '404':
            return 'Not Found'
        else:
            return 'Error'

After:

def interpret_response(response):
    if response.status == '200':
        return response.data
    elif response.status == '404':
        return 'Not Found'
    else:
        return 'Error'

Explanation:

Flattening if statements nested within else clauses generates code that is easier to read and expand upon.

Back to top