4 ms·
I disagree -- it probably is not immediately understood by most programmers. The main flaw of the technique is that the author uses the '&&' not as a logical o
by psyklic 16y ago
I disagree -- it probably is not immediately understood by most programmers.
The main flaw of the technique is that the author uses the '&&' not as a logical operator (the intuitive use) but as a conditional operator.
'&&' is the 'Logical AND'. The statement "foo() && bar();" just doesn't make initial sense, because the logical result of the expression is thrown out.
After thinking this, the programmer probably will realize that the functions are actually called to evaluate the expression. And then finally (hopefully) the programmer realizes that they are evaluated short-circuit.
So, the code may be functional but it is not very readable.
- leokote 16y ago"the expression evaluates to true if both return True, but then why is the result thrown out" Actually, the result is not thrown out: the && operator produces the value of its first operand if the first operand is falsy, otherwise it produces the value of the second operand. For what it's worth, there's also a procedural reading of "foo() && bar()" along the lines of "fill out this form AND win a prize!".
- psyklic 16y agoI wrote 'thrown out' because the statement is "A && B;" -- the result is unused due to the terminating semicolon.