Home / Deeply nested if statements

I dislike deeply nested C\C++ code. I.e. the code of the form:

if (foo) {
  if (bar) {
   if (anotherVariable) {
   }
  }
}

It’s a simplified example that doesn’t really show the problem with such code - when the logic within an if is long, such code becomes hard to read because it becomes difficult to figure out what logic condition is handled by a given part.

To my surprise I seem to be in minority. Most people, especially the Windows GUI kind, nest logic conditions like there’s no tomorrow. I find it hard to argue about the superiority of avoiding nesting because saying “this is hard to read” is too vague. But I’m relieved to find out that I’m not the only one that has a dislike for unnecessary nesting. Stepanov, inventor and implementor of STL, has a great example of simplifying nested logic in his “Professionalism in Programming” papler (PowerPoint, PDF, from his website).

The way to avoid deeply nesting is to do early exit as soon as possible. The trivial example could be rewritten as:

  if (!foo) return;
  if (!bar) return;
  if (!anotherVariable) return;

  ... and this is the logic

Of course trivial examples teach us nothing - this only shows the forest so that you don’t get lost in details. For a real-life example see how Stepanov refactors deeply nested logic expression (pages 4-13 in the paper above).