I will add, this bug occurred essentially because of the following logic bug; 01 if (!A || !B) {
02 if (!B) {
03 fix(B);
04 return 1;
05 }
06 return 0;
07 }
08 return 1;
It’s interesting to think about the IF on Line 02.It could have read “if (A)” and that would have been correct, but IMO even better to write out “if (A && !B)” in case anything changes with the surrounding code later. The problem of course is that “A” and “B” are expensive functions which you don’t want to call twice, and it just is so annoyingly verbose to have to assign booleans and then compare those... You almost want to be able to write out a truth table based on calling the functions and then handling their return values. A kind of 2D switch statement; switch (A, B) {
case 1,1:
return 1;
case 1,0:
fix(B);
return 1;
default:
return 0;
}
Does any language support a multi-dimensional switch statement like this?Hmm, maybe it’s not really any better that way either. The case statements are just not explicit enough. |