July 2010 Archives

Indentation Is Evil

| 2 Comments

I'm constantly refactoring existing code; it's my one-man battle against entropy. One change I'll make to lots of existing code is to remove as much indentation as I possibly can. Why? Indentation is evil, right up there with vampires and those people on Jersey Shore. Let's hear from Mr. Torvalds on this:


The whole idea behind indentation is to clearly define where a block of control starts and ends. Especially when you've been looking at your screen for 20 straight hours, you'll find it a lot easier to see how the indentation works if you have large indentations.

Now, some people will claim that having 8-character indentations makes the code move too far to the right, and makes it hard to read on a 80-character terminal screen. The answer to that is that if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.
- Linus Torvalds, Linux Kernel Coding Style

What makes indentation evil? Every additional level of indentation is one more thing that you must remember. The more you indent, the harder your job gets. Imagine your code looks like the following:
    for x in mylist:
        if x meets some condition:
            for y in x.list_of_thingees:
                if y meets some condition:
                    do a
                else
                    do b
        else:
            do c

That's pretty common; there's probably code like that lurking all over your codebase. Now imagine that I ask you to add a new feature here. We only want to make a call to a if we haven't already made a call to b for at least one member of x.list_of_thingees. It sounds easy enough, but think about actually implementing it. Where are you going to spend most of your time? Is it in dropping those couple of lines of code in, or is it figuring out just where in the hulking beast of indented craziness to drop those lines into?

My brain is like a stack. Each level of indentation that I have to think about gets pushed onto that stack, and is more for me to manage. When the time comes to maintain that code, not only do I have to load that entire stack up into my brain again, but then I have to traverse multiple times to figure out where my modifications go. I think that analogy made me sound a lot like that Keanu Reeves movie, Johnny Mnemonic, which is totally what I was going for.

If indentation makes code hard to understand and maintain, how do you fix it? Remove the indentation! It sounds scary, like walking around public in the nude. I assume you though that in the end, it's very liberating, just like walking around public in the nude.

Your language surely contains some great keywords that will aid you in your fight against needless complexity. Here's an example of the above code rewritten using one of these magic keywords, continue.

    for x in mylist:
        if x doesn't meet some condition:
            do c
            continue
        for y in x.list_of_thingees:
            if y meets some condition:
                do a
                continue
            do b

Look at all of the indentation saved, and that's with the addition of only one word! There are far more tools you can use here besides just continue, like the break keyword, functional programming ideas like map and filter, the strategy pattern, and, if your language supports them, list comprehensions. I should warn you, though, that utilizing the concepts I just mentioned might have the horrible side effect of causing you to learn more about your language of choice and programming in general.

About the Author

The Art of Delightful Software is written by Cody Powell. I'm a dev manager at Amazon where I work on the Instant Video, Mobile Clients team. Before that, I was CTO at Famigo, a venture-funded startup that helps families find and manage mobile content.

Are you interested in building great mobile apps for Amazon Instant Video? Email me!

Twitter: @codypo
LinkedIn: codypo's profile
Personal blog: Goulash
Email: firstname + firstname lastname dot com

Categories