This is the n-th post I see about accidental object to string conversions, while in like 10y of writing a ton of JS with various frameworks it happened by accident around...3 times. This underlines very clearly how humans are in general, they get obsessed and stuck on the most minor things that appear unusable or something at first glance, and then keep beating that dead horse.
Typescript is great but it gets cumbersome because it really puts an accent on the types more than every other language out there. You have to litter every file with dozens of imports for every type you use, which makes decoupling harder. You end up almost having to write your application code twice, once in type logic and once in actual logic. Once you start doing something with any meaningful degree of complexity the type mess that naturally arises is more silly than C++ templates.
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.
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.
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.
That tile on the floor is actually lava, but only that one, I’ve only lost three guests to it, I don’t know why people keep complaining about it, just step around it, not that hard
Or we could use a contractor that doesn’t just have a lava tile on the floor, or at least puts up a fence so people don’t just fall in
Heh, that's one way of framing to make something appear more important, when I just said it's basically irrelevant to me, I barely noticed it's a case that exists.
Or of the 30 total interesting things to even post about Javascript, 28 or them are unexpected type conversions. So what do you expect exactly from a Javascript post? Sheer boredom? Would that make you happier?
There are a few more running jokes, like the endless amount of abstractization devs like to apply when it comes to JS, resulting in the 0 days since the last JS framework half-joke.
View all comments
89
u/Leonhart93 Mar 20 '24
This is the n-th post I see about accidental object to string conversions, while in like 10y of writing a ton of JS with various frameworks it happened by accident around...3 times. This underlines very clearly how humans are in general, they get obsessed and stuck on the most minor things that appear unusable or something at first glance, and then keep beating that dead horse.