Over a month later, post number two in my “rules of thumb” for developers series. The first post was on writing less code, period. This one is on using less indentation.
What I really mean by this is that I’m strongly not in favor of complex nested code structures. I find them to be difficult to follow when reading code, and to be good places for little logic bugs to hide. How often have you seen code that looks like this?
if (something > 10) {
if (something < 100) {
// Do something
}
else if (something > 150) {
// Do something else
else {
return false;
}
}
else {
while (something < 10) {
// Do some other stuff
}
}
I often see conditional structures three or four levels deep, comprising case
statements, if
statements, and loops of various kinds, and in those cases, it can be very difficult to figure out exactly which conditions have to be in place for a given line of code to be executed.
Let’s look at a really common sort of coding problem:
private boolean doSomeStuff(String str) {
if (str.equals("something")) {
// Perform some tasks.
if (str.length() == 10) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
I would always write that method like this:
private boolean doSomeStuff(String str) {
if (!str.equals("something)) {
return false;
}
// Perform some tasks.
return str.length();
}
Any time there’s a condition that causes code to be skipped, I like to put the return statement above the code that could be skipped. It enables me to avoid a level of indentation and, I think, makes the code more readable. This approach goes hand in hand with the practice I’m going to discuss next, which is to write more methods. I think it’s always better to be able to quickly figure out which code you can ignore when you’re reading through code and trying to figure out why something is happening. In the second version of the method above, it’s easy to see the conditions under which you can ignore most of the code in the method.
It’s particularly important to avoid excess nesting when you’re talking about mixing markup with code, in PHP files, JSPs, ERB templates, or whatever. Excessive nesting can render the source nearly unreadable. It’s almost always better to use includes or other approaches to make it easy to distinguish between the control structures in the page and the markup used to render the page.
This practice may seem pretty obvious, but I don’t think I’ve ever seen it explicitly describe before, and I read a lot of code that includes lots of deeply nested structures. I’d love to hear in comments whether people see any downsides to this approach, because I’m methodist when it comes to writing code this way.
A blog for Tivo users
TV producer Chuck Lorre uses the two second “vanity cards” displayed at the end of shows he produces to publish short essays on whatever he feels like. You can only read the essays if you pause the shows on your DVR. One recent card discussed the then upcoming WSJ article about Lorre’s essay. If that doesn’t make his essays a blog in an unusual format, what would?