Re:Anti-If: The missing patterns (2016)
by TooBrokeToBeg on Wednesday June 27, 2018 @11:44AM
(#17409084)
Depending on the language and values, the latter does not mean the same thing.
<?php
$x = 1;$y = 2;
function test1($x, $y) {
if ($x) {
return $y;
} else {
return false;
}
}
function test2($x, $y) {
return $x && $y;
}
var_dump(test1($x,$y));
echo "\n";
var_dump(test2($x,$y));Since we’re discussing readability, let’s be generous and assume the code behaves the same.
Since otherwise, it’s not a question of readability anymore. (But yes, incorrect transformations are incorrect.)
I agree, there shouldn't be any argument on boolean logic being easier than the branching logic. The other examples are more discussion worthy.
Re:Anti-If: The missing patterns (2016)
by matthewmacleod on Wednesday June 27, 2018 @09:41AM
(#17408000)
I'd definitely say it was. The branch is much clearer and more explicit.
Branches are often important things to consider such that hiding them inside in-line conditional expressions can make a code reader miss a key piece of logic. Use in-line branches with caution.
The former can be understood by anyone with a basic grasp of English grammar, while the latter requires understanding the Boolean operator. So yes, the former is more readable.
Borrowing the variable examples from another comment, do you believe that the former is easier to understand for someone who knows English grammar than the latter? (I'm using Python syntax since we're going for English-like readability and added parantheses to the boolean operation to indicate order of operations for the liberal arts major that we've decided is the target audience of this code):
if isAdmin:
return false
if not isActive:
return false
return true
vs return (not isAdmin) and isActive
Re:Anti-If: The missing patterns (2016)
by tigershark on Wednesday June 27, 2018 @08:25AM
(#17407556)
If someone doesn’t have a basic grasp of Boolean logic then maybe programming is not the right choice..
Doesn't matter. Unless one option leads the compiler to generate better code than the other, it's a matter of aesthetics, not correctness. And since you don't even need to be a programmer to understand what the first one means, it's still easier to read.
An observation that comes from studying foreign languages: you learn “and”, “or” and “not” a long time before “if”.
Re:Anti-If: The missing patterns (2016)
by HolyHaddock on Wednesday June 27, 2018 @10:28AM
(#17408378)
Earnestly: Yes. But there may well be programming tasks or jobs that they are suited to, for which no understanding of boolean logic will make no difference on a day to day basis.
Re:Anti-If: The missing patterns (2016)
by jstimpfle on Wednesday June 27, 2018 @06:27AM
(#17407068)
Huh, is this python-style logical evaluation, i.e. "x && y" need not be a boolean but could be whatever "y" is?
Regarding which way is better, there's not much benefit arguing one way or the other. This is a leaf decision. It doesn't affect the structure of other parts of the code.
Re:Anti-If: The missing patterns (2016)
by hellofunk on Wednesday June 27, 2018 @11:44AM
(#17409079)
I'm curious, what about "x && y" is "python-style" ?
It's been in nearly all languages, and for decades before python existed.
Re:Anti-If: The missing patterns (2016)
by jstimpfle on Wednesday June 27, 2018 @12:29PM
(#17409488)
It was not about "x && y" but about the construct from the parent post as a whole. It never uses y in a boolean context, but only as a value. So I was thinking y could be anything. In python this is how it works:
>>> 0 and 'asdf'
False
>>> 3 and 'asdf'
'asdf'
>>> 3 and ''
''
'and' and 'or' in python are short-circuited as in e.g. C, but it's even more short-circuited! The "last" component is not evaluated in a boolean context. For a complete evaluation you have to use the expression in an if or while statement or apply the bool() function. I would say it's like a monad, if that helps.
Re:Anti-If: The missing patterns (2016)
by hellofunk on Wednesday June 27, 2018 @03:03PM
(#17410840)
What you are talking about has nothing to do with short circuiting.
Re:Anti-If: The missing patterns (2016)
by jstimpfle on Wednesday June 27, 2018 @07:08PM
(#17412682)
Agreed, bad choice of words. It's only somewhat similar. Maybe "lazy" would be a better term. Whatever, man! I hope it's clear what I meant.
It’s not that you’re unclear, it’s that it is irrelevant to the discussion, as you were talking about a fairly trivial and unrelated language feature — and it is also a feature that exists in many languages, hardly unique to python.
Whatever, man. Maybe it was not totally related. Agreed. Maybe I had a bad day. It started as a single line that wasn't even the point of my comment. Ok?
No, but:
if (a) return false;
if (!b) return false;
return true;
May be easier to read than: return !(a || !b);But is it easier than
!a && b
? :)Rename a and b to reasonable names:
a -> isAdmin
b -> isActive
const isDeletableUser = !isAdmin && isActive;
return isDeletableUser;
(Better would be naming the function "isDeletableUser" instead of the intermediate const, but was keeping with the original.)Point is for me that booleans with readable names shows how eliminating ifs becomes not only doable but arguably preferable.
Re:Anti-If: The missing patterns (2016)
by tigershark on Wednesday June 27, 2018 @08:43AM
(#17407653)
Seriously, it took me something like 10 seconds to parse your parent post.
How a negated chain of if else would ever be easier to understand than a much simpler !a && b really defies my understanding...
What about:
return a ? false : b;I still prefer the Boolean logic version.
I’m a heavy user of ternary, but it’s still an “if”. And and Or are (not very much) higher-level operations.
I was being mostly facetious.
Seriously are you saying that
is as easy to read as