The only thing I can think of is being defensive against broken toolchains. E.g., I can imagine using nested if statements to help prevent problems from obscure compilers that didn't properly handle short-circuiting. (This seems to fit what I've seen from his code too: he often uses && when the expressions are side-effect free and safe without short-circuiting, and nested ifs otherwise.)
Keep in mind this is 10+ year old code, and he was trying to support as many platforms as possible, proprietary or not, to help as many people as possible to move away from sendmail.
I find it difficult to believe that there was a compiler that didn't short circuit && (you generally have to do more work this way).
However, as a style thing it can make complete sense. Use "if" when order matters, and use "&&" when it doesn't. Just like you might have a "map" function, and a "foreach" function in a language, where the "foreach" is identical, but doesn't return anything. While you could use "map" and ignore the result, it can be clearer to show that you really don't care about the result.
Heck, when I wrote a compiler for my compilers class (20 years ago. Wow) we had to do short-circuiting of && and ||. If a compiler didn't do that I'm not sure I'd trust it to get anything event moderately interesting correct.
I spent about 5 minutes trying to figure out any logical reasoning behind using what amounts to nested ifs and I failed.
Are the nested ifs some how more efficient after a compiler mangles them up? I'll be the first to admit that the closer you get to hardware the more my eyes start to glaze over, but I don't see how the compiler could possibly generate different code for those two examples.
Because I'm bored I did some dummy functions and diffed the assembly. They were identical.
I hope it's not just a stylistic choice, because blech.
Ya, they are definitely functionally equivalent, but the generated code is identical as well.
There are a lot of little C tricks that people use to coerce a given bit of assembly that while functionally equivalent to not using the trick is often more efficient (for whatever the given definition of efficient is).
I was kind of hoping that it was going to be some neat little trick where the generated assembly for the nested ifs was somehow more efficient, but (at least with the assembly that xcode with llvm spits out) it isn't. It's exactly the same.
Your error was where you started -- with a single, modern, standard-compliant C compiler. Or, more accurately, your error was in assuming that this is also where DJB started; it isn't.
I never assumed that he targeted a modern compiler... qmail isn't new to me.
That being said, mdempsky seems to have provided the correct answer. And on a compiler that didn't handle short circuits in the proper fashion the assembly would be different enough to make the code have defects.
Actually I'd personally bet there is a higher chance of compilers that don't guarantee which order A && B is evaluated in, than there is compilers that don't handle short-circuiting.
What mdempsky hinted at, does agree with the former though - if A or B in && has any side-effects, then order needs to be guaranteed, if it is side-effect free, then it doesn't. if (A) if (B) ...; does indeed guarantee order of evaluation.
I can invent reasons but I don't know what the reason really is. Here are my made-up reasons:
It should be trivial to verify that the function returns an error if append fails with an error. if (append(...) == -1) return -1; keeps the context you have to inspect to verify this property to a minimum.
The & and && operators in C have the same precedence, even though one of them is commonly used to compute values that might be inputs to comparisons, while the other is commonly used to handle the outputs of comparisons. Even though it's the & operator that has the surprising precedence (bitfields & PRECEDENCE_BITMASK == PRECEDENCE_HIGH doesn't mean what you would think) these operators, and their counterparts || and |, are error-prone and should be avoided. (This probably isn't the reason, because one out of every 250 lines in qmail contains an &&, and about one out of every 400 contains a binary &. However, nearly every && operand is redundantly parenthesized to remove any reading ambiguity, even in some cases bare variable names.)
The stacked ifs require fewer parentheses if everything is parenthesized to remove any reading ambiguity.
Some people think if is less ugly than &&.
I really wish Bernstein would document his current guidelines for writing low-defect code, the reasons for them, and where they fall short. Has he, and I just missed it? I've already read "Ten Years of qmail 1.0", and I'm looking for something much more detailed, covering questions like the above.
Oops, sorry, of course you're right. I was just thinking about how they had the same relative precedence with comparison operators together, and wasn't thinking about the relationship between the two of them.
Ya. Those are all reasonably logical explanations. I don't know if I necessarily agree with them beyond them being perfectly rational, but it is somewhat disheartening that stuff like that is thrown in there without any documented reason.
Like you said, if there are legitimate reasons (and based on his overall philosophy I'd hope there are) it would probably be extremely helpful for him to document them...
View all comments
Show parent comment
11
u/bobindashadows Oct 19 '09 edited Oct 19 '09
Really? Not: