News for hackers. Stuff that matters.
Re:JavaScript: The Curious Case of null >= 0 by infogulch on Saturday September 09, 2017 @09:43AM (#15207032)
Dynamic typing is a nightmare.
Re:JavaScript: The Curious Case of null >= 0 by namelost on Saturday September 09, 2017 @10:12AM (#15207166)
It's not so much dynamic typing, but a lack of typing. Dynamic typing means that type checks are made at runtime.

e.g. in Ruby:

    irb> nil >= 0
    NoMethodError: undefined method `>=' for nil:NilClass
and Python 3:

    >>> None >= 0
    TypeError: '>=' not supported between instances of 'NoneType' and 'int'
Obviously JS (and Python 2!) get this wrong, but to be fair JS was designed in the 90s.
Re:JavaScript: The Curious Case of null >= 0 by SomeStupidPoint on Saturday September 09, 2017 @11:00AM (#15207379)
Someone else pointed out that Java has (or had?) a similar issue with comparing boxed Ints -- equality checks for object level equality while inequality compares the values.

Without explicit casting, the equality operator casts to a more general type (or rather, doesn't cast at all) than the inequality operator (which casts to numerics).

This happens anywhere you have weird coercion rules.

Re:JavaScript: The Curious Case of null >= 0 by kbp on Saturday September 09, 2017 @01:16PM (#15207992)
> Java has (or had?) a similar issue with comparing boxed Ints -- equality checks for object level equality while inequality compares the values.

In Java 5 and up, 'new Integer(1) < new Integer(2)' will return true, because it automatically unboxes both Integers and compares them as primitives (before Java 5 it was a compile error).

It will not unbox the Integers in 'new Integer(1) == new Integer(1)', though, because given objects, '==' is always reference equality, so it will return false because they're different instances ('!=' would still return true, though). To compare objects by value, you use equals; 'new Integer(1).equals(new Integer(1))' is true.

Re:JavaScript: The Curious Case of null &gt;= 0 by matt2000 on Saturday September 09, 2017 @12:22PM (#15207746)
I agree with everything you said, except for the part where you said JavaScript was "designed".
Re:JavaScript: The Curious Case of null &gt;= 0 by quickthrower2 on Saturday September 09, 2017 @09:39PM (#15210042)
Haskell was designed in the 90s. So don't blame the decade.
Re:JavaScript: The Curious Case of null &gt;= 0 by kccqzy on Saturday September 09, 2017 @03:44PM (#15208842)
Plenty of languages designed before the 90s got this right. It's not like typing is a recent development.
Re:JavaScript: The Curious Case of null &gt;= 0 by Pxtl on Saturday September 09, 2017 @10:51AM (#15207340)
While I dislike dynamic typing in general, in this case the nightmare comes from aggressive and inconsistent coercions. You could have the same mayhem in a statically-typed language with poorly-considered implicit typecasts.
Re:JavaScript: The Curious Case of null &gt;= 0 by ratboy666 on Saturday September 09, 2017 @11:28AM (#15207532)
No. In SNOBOL4 (one of the first dynamic typing languages) -- ident(x) yields true if x is "null", eq(x) is true if x is 0, gt(x) is ">" and ge(x) is ">=". The program "code" is a repl.

Notice -- free format dynamic typing, and, it gets it "right". This language/run-time was originally from the late '60s, last official update in 1975 (with some updates in the intervening decades).

Yes, according to the Javascript spec, the JS behaviour is correct. However, something as old as SNOBOL4 should have taught us that keeping type converting numerical operators dis-similar from object comparision would be good: "==" and "===" vs ident() and eq() and then relating ge() le() etc to the numeric class of operations consistently.

  : fred@dejah wittgenstein $; code
  The Macro Implementation of SNOBOL4 in C (CSNOBOL4BX)   Version 2.0
      by Philip L. Budne, January 1, 2015
  SNOBOL4 (Version 3.11, May 19, 1975)
  BLOCKS (Version 1.10, April 1, 1973)
    Bell Telephone Laboratories, Incorporated
  EXTENSIONS (Version 0.25, June 16, 2015)
    Fred Weigel

  No errors detected in source program

  CODE (TUE AUG  4 10:28:58 EDT 2015)
  RUNNING ON CSNOBOL4 MAINBOL WITH SPITBOL, BLOCKS, EXTENSIONS
  ENTER SNOBOL4 STATEMENTS (TRY ? FOR HELP)
  5,541,616 BYTES FREE
  CODE: ident()
  SUCCESS
  CODE: ident(0)
  FAILURE
  CODE: eq(0)
  SUCCESS
  CODE: eq()
  SUCCESS
  CODE: gt()
  FAILURE
  CODE: ge()
  SUCCESS
  CODE:quit
  Normal termination at level 1
  code.lss:660: Last statement executed was 938
  SNOBOL4 statistics summary-
          5.384 ms. Compilation time
         90.585 ms. Execution time
          20577 Statements executed, 121 failed
          20004 Arithmetic operations performed
            122 Pattern matches performed
              2 Regenerations of dynamic storage
         29.481 ms. Execution time in GC
              0 Reads performed
             10 Writes performed
       4402.237 ns. Average per statement executed
        227.157 Thousand statements per second
  : fred@dejah wittgenstein $; 
and, keeping in the spirit:

  CODE: ge('a')
  EXECUTION ERROR #1, Illegal data type
  FAILURE
Allow the explicit capture of "type" errors. We knew this in 1975. Why was this forgotten?
Re:JavaScript: The Curious Case of null &gt;= 0 by laichzeit0 on Saturday September 09, 2017 @01:04PM (#15207927)
Lack of formal training in Computer Science. Most programmers are self-taught and know nothing about the history of the field or prior work.
Re:JavaScript: The Curious Case of null &gt;= 0 by gruez on Saturday September 09, 2017 @09:48AM (#15207066)
Done right (like in python), it's not so bad.
Re:JavaScript: The Curious Case of null &gt;= 0 by zzalpha on Saturday September 09, 2017 @09:54AM (#15207092)
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 practical joke that someone accidentally took seriously.

Re:JavaScript: The Curious Case of null &gt;= 0 by alangpierce on Saturday September 09, 2017 @10:27AM (#15207219)
> 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 &gt;= 0 by mikebenfield on Saturday September 09, 2017 @12:19PM (#15207731)
Yes, I've always understood this to be the correct terminology, but unfortunately in informal discussion among non-academically inclined software people, people use the terms wrong more often than they use them right. You get lots of people saying C is strongly typed (it certainly is not) and something like Scheme is weakly typed (it certainly is not).
Re:JavaScript: The Curious Case of null &gt;= 0 by zzalpha on Saturday September 09, 2017 @12:37PM (#15207804)
If you're really want a fun time, try having a conversation about "duck typing" some time... it was around the time that term became a thing that I realized no one seems to agree on what all these terms mean.
Re:JavaScript: The Curious Case of null &gt;= 0 by marcosdumay on Saturday September 09, 2017 @02:50PM (#15208559)
Using "strict" vs. "non-strict" and "static" vs. "dynamic" will increase a lot the odds of people understanding.
Re:JavaScript: The Curious Case of null &gt;= 0 by TheRealPomax on Saturday September 09, 2017 @01:43PM (#15208155)
the problem is there is no "right" until you define what right means, which is where the spec comes in. These results are "right", if unexpected, and the true issue here is of course not so much the mathematical results between the three tests, but the fact that JS gives you the freedom to compare anything to anything, irrespective of whether that comparison makes sense at all. Python allows this in a few places, too, with equally bizarre results (see another comment that highlights a few of them).

While tempting to do so, thinking of >= as implying "> or ==" is simply not true in any language that performs type coercion to get around type incompatibility issues.

Re:JavaScript: The Curious Case of null &gt;= 0 by outoftacos on Saturday September 09, 2017 @09:56AM (#15207102)
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.
Re:JavaScript: The Curious Case of null &gt;= 0 by zzalpha on Saturday September 09, 2017 @10:05AM (#15207141)
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, maybe the ECMA will update the language to fix some of the issues.

Meanwhile, being aware of them is often important in order to build correct, secure code.

And they're just entertaining.

So chill. Let's all laugh at how ugly JS is on a Saturday morning and enjoy ourselves a bit. Because when it comes to the JS ecosystem, if we can't laugh, we'd probably be crying...

Re:JavaScript: The Curious Case of null &gt;= 0 by mrgriffin on Saturday September 09, 2017 @10:18AM (#15207187)
I find this behavior surprising:

  >>> d = {0: "int"}
  >>> d[False] = "bool"
  >>> d
  {0: 'bool'}
  >>> nan = float('nan')
  >>> d[nan] = 0
  >>> d[nan] = 1
  >>> d
  {0: 'bool', nan: 0, nan: 1}
Yes, yes, it's because issubclass(bool, int) == True, and nan != nan, but weird.

And if we include Python 2 you get the truly ridiculous:

  >>> dict() < set()
  True
  >>> dict < set()
  False
  >>> 0 < dict
  True
  >>> 0 < 1j
  TypeError: no ordering relation is defined for complex numbers
To compare e.g. a dictionary and set we compare the _names_ of the types! Except complex numbers, because they don't have an ordering. I actually have come across this as a bug in production.

Personally I'd just make any comparison involving different types an error. That probably interacts poorly with subtyping, but it's a price I'm willing to pay.

Re:JavaScript: The Curious Case of null &gt;= 0 by guitarbill on Saturday September 09, 2017 @10:50AM (#15207334)
The `nan` thing is actually because `float('nan')` can (and does) return a new object (well, with a different ID) each time. So it's like going `d[object()] = 'a'; d[object()] = 'b'` and not retaining the objects for lookup.

> Personally I'd just make any comparison involving different types an error

As you point out, this is what Python 3 did for `None`. But it's been idiomatic to have 0 = False when languages didn't have `False`; so `bool(0) == False` makes sense, but also `sum(boolean for boolean in sequence)` works. So you could argue that for `True` and `False`, meaningful coercion is possible, and in reverse, if zero is false-y and everything else is truth-y (like in C), then that also makes sense. Imagine if you had to cast every number to a boolean explicitly! So I guess having `d[0] == d[False]` is the lesser of evils.

Re:JavaScript: The Curious Case of null &gt;= 0 by moomin on Saturday September 09, 2017 @11:33AM (#15207553)
My favourite of these is Haskell

> "" = [] True

Which makes perfect sense if you know any Haskell, but looks like a JavaScript wat if you don't.

Re:JavaScript: The Curious Case of null &gt;= 0 by alangpierce on Saturday September 09, 2017 @10:20AM (#15207198)
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 especially surprising behavior.

Re:JavaScript: The Curious Case of null &gt;= 0 by zzalpha on Saturday September 09, 2017 @10:47AM (#15207312)
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.
Re:JavaScript: The Curious Case of null &gt;= 0 by carussell on Saturday September 09, 2017 @10:33AM (#15207239)
> 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 &gt;= 0 by apenwarr on Saturday September 09, 2017 @10:44AM (#15207295)
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 < 0
    True
    >>> None <= 0
    True
    >>> 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 &gt;= 0 by AlphaSite on Saturday September 09, 2017 @10:37AM (#15207260)
Is this python 2 or python 3, afaik that was fixed in python 3.
Re:JavaScript: The Curious Case of null &gt;= 0 by sdeframond on Saturday September 09, 2017 @10:43AM (#15207286)
This error has been fixed in Python 3.
Re:JavaScript: The Curious Case of null &gt;= 0 by zzalpha on Saturday September 09, 2017 @10:44AM (#15207294)
Nope, that's pretty ridiculous!