This is a simple control-flow GOTO question, nothing about resource allocation.
There are two levels of checking if a piece of data is "good". If and only if it passes the first check, we do the second check. If the data fails either test, we use a default value instead.
The second check involves a few steps with intermediate data, so we can't just put it in a short-circuited ||
condition. Furthermore, if the second test passes, we use the output of the second test instead of the original data.
This is the inner loop in a real-time processing situation, so efficiency is very important. We don't want to do any of the calculations more than once.
if (firstCheck(data)) {
result = analyze(data);
if (secondCheck(result)) {
use_result(result);
}
else {
goto FAIL;
}
}
else {
FAIL:
use_result(DEFAULT_VALUE);
}
This GOTO seems to satisfy all my requirements with maximal efficiency. I can think of some other ways to do it, but all would involve extra storage or conditionals. I am wary of GOTOs though. In fact, if I use this, it will be the first time I have ever used a GOTO. So please, help me find the way out!
Use continue
to go to next loop iteration
if (firstCheck(data)) {
result = analyze(data);
if (secondCheck(result)) {
use_result(result);
continue;
}
}
use_result(DEFAULT_VALUE);