News for hackers. Stuff that matters.
Re:Anti-If: The missing patterns (2016) by moomin on Wednesday June 27, 2018 @06:18AM (#17407039)
I guess if you’re not familiar with Boolean operators that would be a thing?

Seriously are you saying that

    if (x) {
        return y;
    } else {
        return false;
    }
is as easy to read as

    return x && y;
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));
Re:Anti-If: The missing patterns (2016) by moomin on Wednesday June 27, 2018 @01:20PM (#17409991)
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.)

Re:Anti-If: The missing patterns (2016) by carlmr on Wednesday June 27, 2018 @06:52AM (#17407174)
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.
Re:Anti-If: The missing patterns (2016) by tabtab on Wednesday June 27, 2018 @11:32AM (#17408961)
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.
Re:Anti-If: The missing patterns (2016) by krapp on Wednesday June 27, 2018 @06:33AM (#17407094)
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.
Re:Anti-If: The missing patterns (2016) by ummonk on Wednesday June 27, 2018 @11:01AM (#17408659)
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..
Re:Anti-If: The missing patterns (2016) by krapp on Wednesday June 27, 2018 @07:24PM (#17412762)
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.
Re:Anti-If: The missing patterns (2016) by moomin on Thursday June 28, 2018 @09:22AM (#17416612)
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.
Re:Anti-If: The missing patterns (2016) by hellofunk on Thursday June 28, 2018 @03:21AM (#17414748)
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.
Re:Anti-If: The missing patterns (2016) by jstimpfle on Thursday June 28, 2018 @06:43AM (#17415709)
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?
Re:Anti-If: The missing patterns (2016) by d--b on Wednesday June 27, 2018 @07:10AM (#17407248)
No, but:

    if (a) return false;
    if (!b) return false;
    return true;
May be easier to read than:

    return !(a || !b);
Re:Anti-If: The missing patterns (2016) by moomin on Wednesday June 27, 2018 @07:13AM (#17407257)
But is it easier than

    !a && b
? :)
Re:Anti-If: The missing patterns (2016) by ballenf on Wednesday June 27, 2018 @10:43AM (#17408506)
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...
Re:Anti-If: The missing patterns (2016) by bena on Wednesday June 27, 2018 @10:49AM (#17408556)
What about:

    return a ? false : b;
Re:Anti-If: The missing patterns (2016) by moomin on Wednesday June 27, 2018 @01:18PM (#17409969)
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.

Re:Anti-If: The missing patterns (2016) by bena on Wednesday June 27, 2018 @01:37PM (#17410138)
I was being mostly facetious.
Re:Anti-If: The missing patterns (2016) by Sharlin on Wednesday June 27, 2018 @05:07PM (#17411875)
De Morgan laws are seriously your friends when trying to make complex boolean expressions optimally readable.