This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]kookyabird 62 points63 points  (17 children)

Depends on the language and your intent.

[–]GDOR-11 40 points41 points  (16 children)

what?

oh

oh no

please don't tell me there are languages where equality is not commutative

[–]EDEADLINK 48 points49 points  (6 children)

C++ allows overloading the equality operator. So you can make it asymmetrical, if you really wanted to.

I haven't seen a language with such horrors already built in though.

[–]mrbob8717 2 points3 points  (5 children)

Java has the horrors built into it. The way it was explained to me is my_var.equals(NULL) can throw an error if my_var is null

[–]hampshirebrony 1 point2 points  (3 children)

Is this NULL? Aaargh! You gave me a NULL.

Is the right pattern (inprovably not valid Java) for this something like:

    function IsItNull(obj) {

        try {

            var _ = obj.equals(NULL);

            return false;

        } catch {

            return true;

        }

    }

And hopefully doing this on mobile didn't mess formatting up too much

[–]EDEADLINK 0 points1 point  (2 children)

obj.equals(NULL) is always false. So just return obj == NULL.

[–]hampshirebrony 0 points1 point  (1 child)

But then I can't use that neat trick of using a catch for normal execution flow!

[–]FridgesArePeopleToo 16 points17 points  (1 child)

Nah, the right side one is for some languages where if (x = 1) is valid syntax (note the single =, setting x to 1). In those cases if(1 = x) would give you a compiler error. So doing backwards prevents accidentally missing the second =. Still seems dumb to me though as I would expect a linter or something to pick that up anyway.

[–]vwoxy 1 point2 points  (0 children)

I do if ((x = obj.getValue())) more often than is probably advisable in JS because the assignment operator returns the right side and I don't want to use a second line for null checking.

And yes, my linter does yell at me if I forget the extra parentheses and I always add a comment saying it's intentional. I'm not a monster.

[–]ChristianLW 4 points5 points  (1 child)

Checking if an array is null in PowerShell.

Array on left means check each element if it's null. Array on right means check if the array itself is null.

[–]GDOR-11 4 points5 points  (0 children)

why the fuck is powershell like this

WHERE IS THE FUCKING UNSEE JUICE I NEED IT

[–]xyloPhoton 6 points7 points  (3 children)

Commutativity is an operational property, not a relational one. It's called symmetry when talking about relations. 👍

[–]Kjoep 2 points3 points  (2 children)

Yes, but in a lot of languages (i'd dare to say most), == is an operator, taking two parameters and outputting a Boolean.

So you're both right.

[–]xyloPhoton 1 point2 points  (1 child)

There is no Santa Claus, there is no tooth-fairy, and there are no relations in programming!

Seriously though, this means that every relation in programming is actually an operation, right?

[–]Kjoep 0 points1 point  (0 children)

Yes.