Nested If (x) checks - Better way to write this?

Posted by Will on Stack Overflow See other posts from Stack Overflow or by Will
Published on 2010-05-20T16:41:06Z Indexed on 2010/05/20 16:50 UTC
Read the original article Hit count: 88

Filed under:
|

There are places where I check for valid pointers before I perform an operation with them; these checks can be nested pretty deeply sometimes.

For example, I have

if (a)
{
  if (a->b())
  {
    if (a->b()->c())
    {
      a->b()->c()->DoSomething();
    } 
  }
}

I really don't like the look of this. Is there a way to turn this into something more readable? Ideally,

if (a && a->b() && a->b()->c() )
{
 ...
}

would be great, but obviously would not work.

© Stack Overflow or respective owner

Related posts about c++

Related posts about c