Exiting Early; Nesting Is a Code Smell

Nesting code more than 2 or 3 levels deep is a code smell and should be avoided. Here’s a trick I learned and continue to use everyday.

Let’s say you have:

1
2
3
4
5
def some_function(name):
    if name in DB:
        # long block of code here
    else:
        return "Name not found"

You can transform this into:

1
2
3
4
def some_function(name):
    if name not in DB:
        return "Name not found"
    # long block of code is not nested anymore

I call this pattern “returning early with the inverse condition”. The cool thing is: it stacks. So instead of:

1
2
3
4
5
def some_function(name):
    if name in DB:
        if some_other_condition:
            if not another_condition:
                # 4-levels deep nesting

you now have:

1
2
3
4
5
6
7
8
def some_function(name):
    if name not in DB:
        return "Name not found"
    if not some_other_condition:
        return "Nope, can't do"
    if another_condition:
        return "Sorry mate"
    # long block of code here, not nested anymore

You can also do this in loops:

1
2
3
4
for item in array:
    if not condition:
        break
    # code goes here

or even files:

1
2
3
if some_condition_while_testing == 0:
    exit(0)
# the rest of the code

Perhaps it’s obvious, but I still see a lot of code that could use this change. Indentation matters. First post written.

Cheers.