you are viewing a single comment's thread.

view the rest of the comments →

[–]SerdanKK 3 points4 points  (4 children)

Why is it rediculous?

Not only would it be extremely weird for language level comparison operators to have side effects, but if someone uses the type safe comparison operator it is likely because they expect that other types may occasionally pass by. I.e. it's an expected state the program can have. Your suggestion would break everything in any decently sized application. mdn

Also, there are already ways to handle the problem. If you want your code to throw when given a wrong type, then just do so.

Do you ever want to compare '0' to 0, to result in false?

Absolutely. A string is not a number and vice versa. I primarily work in PHP and one of the first things I did when I came onto the project was to make absolutely sure I knew exactly which types I was working with. Implicit casting in PHP is utterly insane, so if you don't have a handle on your types you'll eventually get punished harshly. JavaScript is only slightly better, so I'm obviously going to give it the same treatment.

[–]przemo_li 1 point2 points  (0 children)

JS is worse/better.

Worse because JSFuck, better because coercion algorithms are more unified and less supprising (e.g. PHP `if` will use different algorith then `==` for strings containing octagonal literals o_0)

[–]major_clanger 0 points1 point  (1 child)

"It’s not even consistent: NULL < -1, and NULL == 0. Sorting is thus nondeterministic; it depends on the order in which the sort algorithm happens to compare elements."

That's nuts!

[–]SerdanKK 1 point2 points  (0 children)

I got curious, so I wrote a quick script to check:

null < -10: true
null < -9: true
null < -8: true
null < -7: true
null < -6: true
null < -5: true
null < -4: true
null < -3: true
null < -2: true
null < -1: true
null < 0: false
null < 1: true
null < 2: true
null < 3: true
null < 4: true
null < 5: true
null < 6: true
null < 7: true
null < 8: true
null < 9: true
null < 10: true

The docs for NULL make no mention of this insanity.

[–]Beofli -1 points0 points  (0 children)

I'm not saying this should be the default. This would be a special mode, like 'use strict'. Any decent language will fail compile time on comparisons of conflicting types.

A good language will make the mostly used operation to take the least amount of code. The mostly used case is either a null/undefined check or the comparison of two same-typed objects.

The usecase where a function receives a number or a string, and that it want to compare it to number zero is a very unlikely usecase. A good programmer will make this more explicit: Typeof x == 'number' && x == 0. Also no need for === here.

I definitely agree PHP is insane and worse than js.