4 ms·
I can't help but compare JS to python though. For every footgun in JS, the same footgun does not exist in python including this one.
by umvi 6y ago
I can't help but compare JS to python though. For every footgun in JS, the same footgun does not exist in python including this one.
- jkaptur 6y agoInteresting - I view it the opposite way. In JavaScript, you need to know the details of exactly one numeric type: the regular, standard IEEE 754 float that's used by Java, Python, C++ (generally), and every other programming language under the sun. That's it! There is only one way to do it ;) These days, if you need huge integers, you can use BigInt, but if you want to use it in an expression that also contains floating point values, then you need to explicitly convert it. Explicit is better than implicit.
- umvi 6y agoI just find it counter intuitive, that's all. The expectation is that high level scripting languages have BigInt enabled by default (since that's what Ruby/Python do). Not so with JS. The expectation is that sorting an array of integers in place: [1,3,2,4,5,7,6,8,9,10].sort() produces an array with the numbers in ascending order (since that's what Ruby/Python do). Not so with JS (try it out). A agree there is a perfectly logical reason for every quirky behavior, but I've been surprised (in a bad way) so many times by JS's counter-intuitive behavior that I now have a mental collection of gotchas where JS deviates from expectation.
- chrisseaton 6y agoConsider this: JavaScript actually has a formal specification. Ruby and Python have no formal specification. I can tell you (with reference to the specification) exactly what a JavaScript program does. JavaScript may seem wacky on the surface, but actually it's all worked out. Nobody can tell you what a Ruby or Python program does. And you think JavaScript is the footgun?
- umvi 6y agoWell yes, in theory if all the developers read JS's formal specification JS would be better. But in practice developers just wing it without reading anything, and only go to read things when getting an unexpected result. And so we get the follow outcome - python behaves as expected in more cases than JS behaves as expected. "Theory is great in theory, but in practice, practice is better"
- jkaptur 6y agoI mean, if you define "whatever Ruby and Python do" to be intuitive, then, sure, every other language is going to have "a long list of weird footguns". JS has many quirks, but I like that the numeric representation is extremely simple and explicit (even compared to Python).
- mschuetz 6y agoThat's a problem with the specification of sort, not with javascript using double by default. But yeah, that sort example is seriously bonkers.
- ficklepickle 6y agoArray.sort converts to UTF-16 code units, or some such weirdness, by default. If you are sorting anything other than strings, you will want to pass in a comparison function. [1,3,2,4,5,7,8,9,10].sort((a,b) => a - b) gives you the behavior one would expect. I suppose you already know that, though. Agreed there are lots of quirks.
- recursive 6y agoExcept it also has bitwise operators which is definitely weird.
- ygra 6y agoAnd those bitwise operators are defined to convert the number into a 32-bit signed integer as part of the process. Which is why foo | 0 is a way of converting a number to an integer (as long as it's not too large ...).
- MaxBarraclough 6y agoI agree Python is the better language, but they have quite different origin stories. JavaScript was thrown together in very short order, and has gradually evolved since then. It was a good enough first-to-market scripting language for the browser, got standardised, and has been the client-side scripting language of the web ever since. It's evolved somewhat since its early days but is backward compatible. If I understand correctly, Python had a more considered initial design, and had a clean-break overhaul in Python3, allowing it to evolve without accumulating as much cruft and inconsistency. (Python is famous for its philosophy of there should be one—and preferably only one—obvious way to do it.) People use Python by choice: unlike JavaScript where browsers support no other scripting languages, Python won out over its rivals in fair contest. All things considered, JavaScript isn't that bad. Used correctly it can be elegant and powerful. It has some nasty dark corners, but it generally provides all you need to avoid them, if you know your way around. With modern JIT engines it can even be pretty fast. Its curious use of doubles is just the kind of thing modern JIT engines are able to be smart about, compiling down to use integer operations where possible.
- andreareina 6y agoPython just has its own footguns, like mutable default arguments or how lambdas close over variables in comprehensions.