Live data from Hacker News

JavaScript: The Curious Case of null >= 0

blog.campvanilla.com

21–30 of 152 posts

Re: JavaScript: The Curious Case of null >= 0

#21
post #6
post #4

Earlier quoted context omitted.

Done right (like in python), it's not so bad.

I really wish the industry settled on terms for this stuff but alas... The difference is python is strict about its types and doesn't do automatic coercion. So at least you get type errors at runtime and not magic implicit behaviour. That said, even Perl did this better (less magic and surprises)... JavaScript is like a language intentionally designed to surprise the developer. Half the time I feel like it's a practi…

> I really wish the industry settled on terms for this stuff

I've pretty consistently heard "strong typing" to mean that the language avoids coercing between types, and "static typing" to mean that types are known at compile time. So JS and Python are both dynamic typed, but Python is (more) strongly typed and JS is (more) weakly typed.

https://stackoverflow.com/questions/2690544/what-is-the-diff...

Re: JavaScript: The Curious Case of null >= 0

#22

Not to defend JavaScript too much, but I think the three examples seem reasonable when viewed in the right way, and I think it's hard for JavaScript to do much better with its existing design philosophy (dynamic typing and coercion rather than runtime errors to deal with type mismatches). >= and > are numerical operations, so both sides are interpreted as a number, and null is 0 when treated as a number. That means `…

[deleted]

Re: JavaScript: The Curious Case of null >= 0

#23
post #11

Earlier quoted context omitted.

Yes I can contrive examples in any language that are nightmarish. Fortunately we manage to muddle through somehow, at least those of us who have work to do in lieu of walking through trivia to prove how smart we are to other hackers.

Okay. Find an equivalently surprising example in... Let's use the other poster's choice, python. I'll wait. Edit: didn't have to wait long, way to go Python! Maybe I'll just claim it probably has fewer of these types of bizarre idiosyncrasies? I hope it has fewer... I'm always baffled by JavaScript apologists... Look, your baby is ugly. We know it's ugly. You know it's ugly. But if we point out the problems, hey, may…

> Find an equivalently surprising example in... Let's use the other poster's choice, python.

> I'll wait.

I don't write Python, but even so it took about 30 seconds, using the same area of the linked article as a starting point:

    >>> 0 > None
    True
At this point, I fully expect some attempt at a post-hoc rationalization for why Python's choice here is unimpeachable.

Re: JavaScript: The Curious Case of null >= 0

#24
post #5

Did he seriously claim at the end that that made sense "mathematically"?

Yes. For X and Y being of the same type, defining the Define X X > Y : X X == Y: !(X X)

X != Y: !(X == Y)

X >= Y: !(X X That's why it's the only operator that needs to be defined for std::map/set (which are rb-trees) in C++

Now, if X and Y aren't the same type, the only thing you can expect to get out of the system is a cronenberg.

Re: JavaScript: The Curious Case of null >= 0

#25
post #11

Earlier quoted context omitted.

Okay. Find an equivalently surprising example in... Let's use the other poster's choice, python. I'll wait. Edit: didn't have to wait long, way to go Python! Maybe I'll just claim it probably has fewer of these types of bizarre idiosyncrasies? I hope it has fewer... I'm always baffled by JavaScript apologists... Look, your baby is ugly. We know it's ugly. You know it's ugly. But if we point out the problems, hey, may…

> Find an equivalently surprising example in... Let's use the other poster's choice, python. > I'll wait. I don't write Python, but even so it took about 30 seconds, using the same area of the linked article as a starting point: >>> 0 > None True At this point, I fully expect some attempt at a post-hoc rationalization for why Python's choice here is unimpeachable.

Is this python 2 or python 3, afaik that was fixed in python 3.

Re: JavaScript: The Curious Case of null >= 0

#26

Not to defend JavaScript too much, but I think the three examples seem reasonable when viewed in the right way, and I think it's hard for JavaScript to do much better with its existing design philosophy (dynamic typing and coercion rather than runtime errors to deal with type mismatches). >= and > are numerical operations, so both sides are interpreted as a number, and null is 0 when treated as a number. That means `…

Aggressive coercion in an attempt to execute clearly faulty code is exactly the problem.

Re: JavaScript: The Curious Case of null >= 0

#27
post #11

Earlier quoted context omitted.

Okay. Find an equivalently surprising example in... Let's use the other poster's choice, python. I'll wait. Edit: didn't have to wait long, way to go Python! Maybe I'll just claim it probably has fewer of these types of bizarre idiosyncrasies? I hope it has fewer... I'm always baffled by JavaScript apologists... Look, your baby is ugly. We know it's ugly. You know it's ugly. But if we point out the problems, hey, may…

> Find an equivalently surprising example in... Let's use the other poster's choice, python. > I'll wait. I don't write Python, but even so it took about 30 seconds, using the same area of the linked article as a starting point: >>> 0 > None True At this point, I fully expect some attempt at a post-hoc rationalization for why Python's choice here is unimpeachable.

This error has been fixed in Python 3.

Re: JavaScript: The Curious Case of null >= 0

#28
post #11

Earlier quoted context omitted.

Okay. Find an equivalently surprising example in... Let's use the other poster's choice, python. I'll wait. Edit: didn't have to wait long, way to go Python! Maybe I'll just claim it probably has fewer of these types of bizarre idiosyncrasies? I hope it has fewer... I'm always baffled by JavaScript apologists... Look, your baby is ugly. We know it's ugly. You know it's ugly. But if we point out the problems, hey, may…

> Find an equivalently surprising example in... Let's use the other poster's choice, python. > I'll wait. I don't write Python, but even so it took about 30 seconds, using the same area of the linked article as a starting point: >>> 0 > None True At this point, I fully expect some attempt at a post-hoc rationalization for why Python's choice here is unimpeachable.

Nope, that's pretty ridiculous!

Re: JavaScript: The Curious Case of null >= 0

#29
post #11

Earlier quoted context omitted.

Okay. Find an equivalently surprising example in... Let's use the other poster's choice, python. I'll wait. Edit: didn't have to wait long, way to go Python! Maybe I'll just claim it probably has fewer of these types of bizarre idiosyncrasies? I hope it has fewer... I'm always baffled by JavaScript apologists... Look, your baby is ugly. We know it's ugly. You know it's ugly. But if we point out the problems, hey, may…

> Find an equivalently surprising example in... Let's use the other poster's choice, python. > I'll wait. I don't write Python, but even so it took about 30 seconds, using the same area of the linked article as a starting point: >>> 0 > None True At this point, I fully expect some attempt at a post-hoc rationalization for why Python's choice here is unimpeachable.

python 2 made a rational, well-considered choice, though I do like the python 3 replacement better. Despite its self-consistency, python 2 ended up causing hard-to-find errors (mostly when people accidentally compared "0" with 0 though; it mostly wasn't a problem with None).

In python 2, cross-type comparisons are strictly ordered. So:

    >>> None == 0
    False
    >>> None >> None >> None > 0
    False
Completely consistent, no special cases, and if you sort() a list containing multiple types, it'll always come out in an unsurprising, consistent order.

Re: JavaScript: The Curious Case of null >= 0

#30
post #11

Earlier quoted context omitted.

Okay. Find an equivalently surprising example in... Let's use the other poster's choice, python. I'll wait. Edit: didn't have to wait long, way to go Python! Maybe I'll just claim it probably has fewer of these types of bizarre idiosyncrasies? I hope it has fewer... I'm always baffled by JavaScript apologists... Look, your baby is ugly. We know it's ugly. You know it's ugly. But if we point out the problems, hey, may…

I gave a talk on Python idiosyncrasies a while back that you might enjoy: https://speakerdeck.com/alangpierce/python-puzzlers Several of the examples from that talk have equivalents in JavaScript where JavaScript does better. I think pretty much any real-world language has lots of surprises like this that you need to get used to, although I certainly admit JavaScript (particularly JS type coercion) tends to have espe…

I'd say JSs biggest problem is that the surprising behaviours aren't mostly sitting in dark corners waiting to bite you. They're standing there in the open, where everyone is likely to encounter them.
Post reply on HN