I was wondering if there was a better way of handling the case in C where you want to exit a function as soon as you encounter an error in a series of expressions. (in this case, its a function that returns a NULL on error)
e.g. in some C code where they tried to short circuit error handling by combining a series of statements with AND (&&).
return doSomething() &&
doSomething2() &&
doSomething3() && ... ;
This irritates me since we're cramming so much on one line in one statement. But I guess the alternative is
if (!(value = doSomething()))
return NULL;
if (!(value = doSomething2()))
return NULL;
etc
return value;
But what about short circuit error evaluation that I've seen in perl and bash scripts..
int die(int retvalue) {
exit(retvalue);
}
.....
(value = doSomething()) || die(1);
(value = doSomething2()) || die(2);
(value = doSomething3()) || die(3);
return value;
The main problem with this is that the RHS has to be an expression so you can't really goto or return from the function. Would anyone find this of value or is it too limited?
edit: I guess I should have meant to include newlines in the first example. The problem is that you need to be careful if you decide to add another expression in the middle.