I see what you’re saying, and this example is clearly made to highlight an extreme case, but wouldn’t the language be better if it didn’t do weird object conversions like this?
I see the argument a lot that while writing JS you don’t often encounter these language quirks as long as you structure your code well, but I tend to stand behind the idea that it would’ve been better for the language to not allow you to do these things in the first place.
JavaScript is a great language but it has parts of the spec that were really quirky decisions. Unfortunately, things like conversion to [object Object] are indeed specified behavior, not undefined behavior, and are therefore unlikely to ever change. So we just have to live with this stuff now. But as the previous commenter mentioned, in my 7 years of writing JS I’ve been tripped up by zany type conversions only maybe 3 or 4 times. Not a prevalent problem for most JS devs.
No. It's not. It's dogshit, always has been dogshit, and always will be dogshit. Fixing it would require literally making a new language from the ground up because many of its problems are so core you actually can't get rid of them.
The actual weird part of this thing, though, is the complete and total lack of an alternative language to javascript in the web domain. Various orgs have tried to invent alternatives here and there and the always fail because either Google or Microsoft refuse to adopt any solution that they do not have full control over.
Not because they feel the need for control in order to ensure quality. No.
Because they want full control for licensing purposes. Neither is willing to adopt a solution that the can't make money off of.
Cool story, do you know how many times I tripped up by any type conversions in 7 years? ZERO times because I use statically and strongly typed languages sooo. I love seeing those excuses “Reee JS good this is just skill issue” instead of knowing that it is not skill issue but rather language design issue…
Not everyone has a choice of what language they code in, you need a job and take a decent one where everything is in JS... you have to work in JS. You can try to find one in another language but ya boi gotta eat
Users of a programming language are able to find the edge cases in much the same way that users of an application can, but finding edge cases in JavaScript doesn't mean you will have a negative development experience or that the language is unable to efficiently complete tasks.
It's the same situation for JS and Python. JavaScript was originally intended for simple scripting for webpages, in earlier times when we couldn't imagine just how powerful 21st century websites would become, while Python was intended to be simple. Strict typing was either considered unnecessary or undesired. It was a conscious design choice, and back then the concept of good programming practices and engineering wasn't very mature yet.
Fast forward to today, and strict typing is more important than ever for good code and such. NodeJS and Python are among the world's most popular languages now, however since typing wasn't originally part off the language's design, they've to retrofit and introduce it in a roundabout way which won't break legacy code.
In other words, JS does weird stuff like this because of old design decisions. It's always legacy, legacy, legacy.
At least Microsoft got some of the best language developers in the world for TypeScript, and though it does have a few rough edges, overall they’ve done an astonishing job rationalizing the insanity we started with.
Python typing is just Geneva Suggestions. It's added to help developers in the IDE, but strict typing is still considered against the Python philosophy.
It's not so bad tbh, for the most part, but boy do I wish there's better support for defining DataFrame type definitions.
Yes, people shouldn’t make excuses for stupid behaviour like this. But at least if you look at any APIs added to the platform in the last five or ten years, they throw errors like you’d hope a sane language would. And any responsible developer these days will be using typescript and avoid 99% of these issues ahead of time. It’s a pretty good experience if you’re using modern tools, but the legacy baggage is going to be with us forever.
Being able to declare a new object with {}? Having methods expecting strings automatically convert objects to their string representation? having the default string representation of objects being [object Object]?
Huh? I can't say about "most", but most that I am aware of or used, no.
Also, even if that was true. That's still very error-prone thing, even if everyone else does it. While I don't agree that JS is really bad language overall, this shit is bad.
I mean, I've only done Ruby and C# in my professional life, but they both do it and I know from school that Java does it and I think python does too? I don't know, those could be outliers and be the only ones who does this.
I don’t think any of the languages you’ve named do this to the same extent as JavaScript. I could be wrong, but if I recall correctly Java and C# will coerce to a string if you’re concatenating, but will still throw an error if you’re passing to a function that expects a string (like this case in JavaScript). I don’t think Python implicitly coerces anywhere (outside of functions like print and explicit string interpolation). I’m not sure about Ruby, but I think it only coerces certain string-like types in certain contexts.
Old JavaScript would coerce everywhere imaginable because it really didn’t want to throw errors.
The languages that maybe do similar with I can think of are Perl and Php.
No. Some old bad ones do. Modern ones only do in specific contexts where string interpolation is explicitly indicated or the result is immediately meant for displaying to a user. Automatically converting when you call print makes sense since the functions role is converting from structured data to human output. Creating a regex matcher is not a case where that makes sense.
Also consider that every time you see some weirdness in js that shouldn't really exist, there are developers behind the dozens of js implementations out there who routinely, every day, have to consciously force their js interpreters to behave like this, to comply with the open standard, no matter how stupid they think it is.
I do use typescript, but for me it ends up being the same result jst with more work. I never felt like it was a game changer since it happened very rarely that I got errors because I confused the types. I couldn't have used pure JS for so long if I got endlessly stuck due to the lack of specified types.
View all comments
Show parent comment
69
u/FedExterminator Mar 20 '24
I see what you’re saying, and this example is clearly made to highlight an extreme case, but wouldn’t the language be better if it didn’t do weird object conversions like this?
I see the argument a lot that while writing JS you don’t often encounter these language quirks as long as you structure your code well, but I tend to stand behind the idea that it would’ve been better for the language to not allow you to do these things in the first place.