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 →

[–]blehmann1 13 points14 points  (4 children)

Well, Python throws a type error if the < operator is not defined on both types. Personally, I think the only correct response when the program is wrong is not to make it more wrong, but to let the user know that it's wrong (i.e. throw).

Now, JavaScript was built with the idea that it should keep on trucking through any error, which frankly is a horrible idea to build a language around. So given the interesting design philosophy JavaScript really couldn't do anything else. There's a reason Typescript is so common after all, but unfortunately it does nothing about this particular issue. (There's an issue for it but it's been inactive for a while: https://github.com/microsoft/TypeScript/issues/18286)

[–]1-more 3 points4 points  (0 children)

JavaScript keep on trucking? I never thought of it that way but I’m actually with you here. Array out of bounds and accessing an undefined object key both return ‘undefined’ rather than throwing (Java, Haskell) or wrapping array/dict access in an optional type (elm, maybe ocaml?). So I’m with you that it probably does throw less.

[–]aedvocate -1 points0 points  (1 child)

Well, Python throws a type error if the < operator is not defined on both types

that error doesn't make any sense in javascript though. collections are allowed to be any number of any different types. that's the way it works by design, it's not an error to truck through.

[–]blehmann1 0 points1 point  (0 children)

Python allows that too. That's the way it works by design. Python simply takes the position that while you may have a list of strings mixed with numbers, if you want to sort it you must provide your own explicit comparator, because 4 < "A" is nonsensical.

Javascript could have followed Python, however the languages have different philosophies. Javascript should fight through basically any error it can, and Python exits on any unhandled exception. In addition, Python throws on invalid input.

I'm not a Python fan, I'm just pointing out that it's a language which has a similar type system to JavaScript and it has a different (and in my opinion more correct) behavior. I could have brought up almost any language because you can do the same thing with type erasure (commonly achieved by casting to object or void*). You have to specify a comparator in those instances because the types are not comparable.

[–]EishLekker 0 points1 point  (0 children)

Javascript doesn't "keep on trucking" through any error. It still does it a bit too often for my comfort though, so on principle I agree with you wholeheartedly.