惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
博客园_首页
U
Unit 42
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
IT之家
IT之家
G
Google Developers Blog
L
LangChain Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
爱范儿
爱范儿
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
Jina AI
Jina AI
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
小众软件
小众软件
H
Help Net Security

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Never Nesting by Nev R. Nester
Alex Neal Al · 2026-05-01 · via DEV Community

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.