by zaroth · 6y ago · 0 comments
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.
fn main() {
for i in 1..102 {
match (i % 3 == 0, i % 5 == 0) {
(true, true) => println!("FizzBuzz"),
(true, false) => println!("Fizz"),
(false, true) => println!("Buzz"),
(false, false) => println!("{}", i)
}
}
}
Not only is it obviously correct, but if you somehow manage to forget a case the compiler will tell you. {
if A && B return println("FizzBuss");
if A && !B return println("Fizz");
if !A && B return println("Buzz");
if !A && !B return println("{}", i);
}It also usually helps to make the code more self-documenting
Re: self-documenting, certainly sometimes (I mean, you shouldn't play code golf...), but in this case the code in question was:
2218 if (!valid_localpart(maddr->user) ||
2219 !valid_domainpart(maddr->domain)) {
....
2234 return (0);
2235 }
2236
2237 return (1);
It's not obvious to me what you would name the intermediate booleans that would be more self-documenting than the function names used there. save_A = A;
save_B = B;
if (!A)
fix(A);
if (!B)
fix(B);
return (some_expression of save_A and save_B);match (a, b) with | (1, 1) -> 1 | (1, 0) -> (fix b); 1 | _ -> 0
switch (!!a) & ((!!b) < 1) { case 3: return 1; case 1: fix_b(); return 1; default: return 0; }
If you rewrite 3 as “A & B”, this turns into feature flags, and is less crazy looking (not sure arithmetic is allowed in case statements though).
Better (regardless of language) is flow like this:
if (!b) fix_b(); return a && b;
*easy to sit here and armchair make claims of course. I've also only read this post and not the actual code so maybe there is reason for doing it how they did.
I'd add for writing the fix domain logic, probably most clear to fix it in one step, and then do the validation as a second step, instead of mixing the two. Minisculey less efficient but its much easier to follow if fixing and validating logic aren't mixed together.
I am going to guess they will be smart enough to redesign this for the next major release, and the current patch is just that, a patch for the old design.
However, as you yourself hinted, the cost (in C anyway) is giving up the short circuit - which might be expensive, or worse - might have unwanted side effects.
I'm not aware of a common language construct that would switch/match on both A and B, but defer executing B unless its value is actually needed.
if (A && B) {
return 1
else if (A && !B) {
fix(B)
return 1
else {
return 0
}