Nested If (x) checks - Better way to write this?
c, c++
Solution
Why would the latter not work?
In C, && is a short-circuit operator, so it is evaluated from left to right, and if any evaluation is false, evaluation stops.
In fact, you could write:
a && a->b() && a->b()->c() && a->b()->c()->DoSomething();
Problem
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. EDIT - nvm the example that I put up DOES work as everybody has pointed out. I did test it out to see if this works, but there was a bug in my code in my test. duh!