DEV Community

Cover image for Never Nesting by Nev R. Nester
Alex Neal Albinda
Alex Neal Albinda

Posted on

Never Nesting by Nev R. Nester

Never Nesting

I once came across this video around a year ago, and I still think about it whenever I code. Never nesting is essentially never nesting past 3 indents (not strict at all, but as a recommendation.)

As Linus Torvalds says:

... if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.

void process_order(User *u, Order *o, Payment *p) {
    if (u != NULL) {
        if (o != NULL) {
            if (p != NULL) {
                if (!u->is_banned) {
                    if (o->total > 0) {
                        if (check_inventory(o)) {
                            if (p->balance >= o->total) {
                                if (execute_payment(u, o, p)) {
                                    if (finalize_order(o)) {
                                        if (send_receipt(u)) {
                                            printf("Success\n");
                                        } else {
                                            log_error("Receipt failed");
                                        }
                                    } else {
                                        log_error("Finalize failed");
                                    }
                                } else {
                                    log_error("Payment failed");
                                }
                            } else {
                                log_error("Insufficient funds");
                            }
                        } else {
                            log_error("Out of stock");
                        }
                    } else {
                        log_error("Order empty");
                    }
                } else {
                    log_error("User banned");
                }
            } else {
                log_error("No payment info");
            }
        } else {
            log_error("No order info");
        }
    } else {
        log_error("No user info");
    }
}
Enter fullscreen mode Exit fullscreen mode

This looks stupid.

Nesting if conditions or loops get a little complicated to understand for a programmer after a certain point. Take a look at the code snippet above, for example, where you have to take a few minutes to really understand what's happening, to the point where it almost looks obfuscated intentionally. (which it is, technically, i made that up)

A more realistic example looks like this:

// calculator function
int calculate_final(int a, int b, char op) {
    if (a != 0) {
        if (b != 0) {
            if (op == '+') {
                return a + b;
            } else {
                if (op == '-') {
                    return a - b;
                } else {
                    return 0;
                }
            }
        } else {
            return -1;
        }
    } else {
        return -1;
    }
}
Enter fullscreen mode Exit fullscreen mode

Observe how it becomes more difficult to understand what the different outcomes of the function are as you try to read it.

Extraction

Extraction is a method under Never Nesting that involves extracting parts of the code into their own methods/functions.

We essentially abstract code that can be abstracted into their own methods and apply them to the function.

int is_invalid(int a, int b) {
    return (a == 0 || b == 0);
}

int calculate_final(int a, int b, char op) {
    if(is_invalid(a, b)){
        return -1;
    }
    if (op == '+') {
        return a + b;
        if (op == '-' {
            return a - b;
        }
    }
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Here we take the first clause as its own method is_invalid and substitute it into the function.

Guard Clauses

Guard clauses are ways to handle "bad" or edge cases first. If you pass the guards, we can assume that you have good enough data.

In the previous section, we added a function called is_invalid, this is an application of guard clausing, as we allow it to catch the cases wherein a == 0 or b == 0. This reduces the indents we have by a lot, considering that the first clause does not enclose the other operations.

If we apply this to the rest of the code, we get to see this result:

int is_invalid(int a, int b) {
    return (a == 0 || b == 0);
}

int calculate_final(int a, int b, char op) {
    if(is_invalid(a, b)){
        return -1;
    }
    if (op == '+') {
        return a + b;
    }
    if (op == '-' {
        return a - b;
    }

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Should I always Never Nest?

It's impossible to NEVER nest past 3 indents, but limiting yourself to 3 indents keeps your code readable.

The whole point of limiting yourself to 3 levels of depth is for you to rethink your steps if your code just starts looking really stupid and unreadable.

Keep your code flat and readable, it'll end up biting you in the end anyways.

Top comments (0)