This question has been bugging me for a while
Is it usual to have a piece of code that works like this:
bool failed = false;
if (ptr)
{
if (ptr->value == foo)
{
print("error");
failed = true;
}
}
if (!failed)
{
print("all systems go");
}
Or can it be done with out the bool?
I guess the first two ifs could be on one line but im not sure what order they are checked in different environments and I think it might make it harder to read.
All compilers should follow the same precedence order, so the check can be replace by this:
If the first part (the lone
ptr) is false, then the rest of the expression will not be evaluated. This is called short-circuiting.