r/ProgrammerHumor • • Mar 20 '24

Meme areJSDevsActuallySane

Post image
14k Upvotes

554 comments sorted by

View all comments

Show parent comment

57

u/red286 Mar 20 '24

But I can't see any justification for new RegExp to indiscriminately coerce any input to a string.

You can't see any justification for a largely JIT-based language to automatically cast any input to a string when the only possible inputs the function takes are strings?

106

u/Hrukjan Mar 20 '24

Using a JIT-compiler has nothing to do with being weakly typed.

61

u/Some-Guy-Online Mar 20 '24

Correct. If it's not already a string, it should (logically) not be coerced into a string.

String coercion makes a lot of sense in a lot of cases, but mostly when the ultimate use of the variable is as a string.

In the case of RegExp, the goal is not to have a string like "Hello World", the goal is to have a regular expression that can be processed.

So casting any non-string to a string to a string is just asking for an infinite supply of broken edge cases.

when the only possible inputs the function takes are strings?

This part is technically wrong, but whatever. It takes regex literals and other RegExp objects, which is reasonable, but it could check to make sure the input is one of these three things and throw an error for anything else.

36

u/LucasRuby Mar 21 '24

A big part of JS philosophy is to avoid throwing errors as much as possible, because of its initial intended uses in web pages. They simply didn't want a whole web page to break and not be rendered because someone made a type error somewhere and now nothing is displayed but an error page.

Rather, you can see most of the page fine and maybe that image that you forgot to pass properly to the element that should display it will simply show as "[object Object]" and the rest of the page looks fine.

17

u/Salanmander Mar 21 '24

It would honestly be better if a RegExp object that was passed a thing that can't be reasonably parsed as a regex simply always returned false when asked whether something matches it.

Better still would be error throwing with built-in error handling that allowed for things like some of your code running when other parts break.

11

u/LucasRuby Mar 21 '24

First option is a good idea, but frankly the authors of this method probably didn't consider it that far. They just wrote the method to assume everything thrown at it would be a string (if not a regex) and the exceptions got coerced into one. Which is how most JS code will work when you don't actively handle the wrong type parameters.

Second option is still a problem, JS has error handling but you have to actively use it. Wrapping every method of your code in try-catch (or multiple try-catches, since we wouldn't want one error somewhere to break unrelated code) ends up becoming boilerplate and bureaucratic.

2

u/Salanmander Mar 21 '24

What I mean by built-in error handling that would be better than this, is I would rather the language had an automatic

try{}catch{log(error)}

around every method unless otherwise specified. Other stuff that relied on the method would still break and propagate those errors, but it would be breaking anyway.

-3

u/Exaskryz Mar 20 '24

If I had a number, I'd be confused that it didn't go to string though when I regExp for a digit.

12

u/Some-Guy-Online Mar 20 '24

I honestly hope you're joking, but I can't tell.

-6

u/Exaskryz Mar 20 '24

A) Yes, I don't code in JS.

B) Regardless, it is a perk of JS to be predictable. Suddenly throwing errors that say the number 244 does not contain "2" nor "\d" because we forgot to toString() because of a type mismatch would be cumbersome.

8

u/GuitarOk75 Mar 20 '24

A perk of JS is being predictable?! 😂

It's literally anything but. Hence all the memes and this very post.

→ More replies (2)

3

u/Some-Guy-Online Mar 21 '24

This is the kind of nonsense that comes from people who accidentally choose the wrong side in a debate but can't admit when they're wrong. They just end up looking dumb.

→ More replies (7)
→ More replies (4)

19

u/thirdegree Violet security clearance Mar 21 '24

Being jit has nothing to do with js having insane coercion tolerance. That's purely a language design choice. And while yes I know the justification for it, imo it's absolutely not worth the cost. This is why typescript has gained prominence.

2

u/dagbrown Mar 21 '24

It's not casting anything.

What it's doing is taking the representation of an empty object, as should be used only for debugging purposes, treating that as a string, and then going on to parse that debugging representation string as a regex. It's absolutely bonkers.

It's like someone in Python going "oh, this object doesn't have a __str__ method defined, but I really need a string! Should I raise an exception? Fuck it, no, I'll just get whatever string I need out of __repr__ instead!"

1

u/[deleted] Mar 21 '24

Doesn't str default to repr when not defined though?

1

u/SoInsightful Mar 21 '24

Absolutely not. BigInt({}) throws "Uncaught SyntaxError: Cannot convert [object Object] to a BigInt".

The only actual explanation for the coercion is "it seemed like a reasonable idea at the time".

1

u/_xiphiaz Mar 21 '24

It is actually a very reasonable behaviour for how js works. JS when it encounters an object but is typically expecting a string will call the toString() method which is present on all objects. The base case is to return “[object Object]” unless overridden. This can be useful in some albeit niche circumstances, but more importantly it isn’t inconsistent.

The reason your example with BigInt doesn’t work is because a vanilla object doesn’t implement the valueOf method. You will find for example that you can pass a Date to BigInt and it will happily construct due to having valueOf implemented

1

u/SoInsightful Mar 21 '24

No, it's not reasonable behavior. It's in line with how JS works much of the time, but that doesn't make it reasonable behavior to begin with.

Here are some examples that rightfully don't work in JS:

Symbols fail despite having both valueOf and toString:

new RegExp(Symbol())
// Uncaught TypeError: Cannot convert a Symbol value to a string

Functions fail despite having both valueOf and toString:

BigInt(() => {})
// Uncaught SyntaxError: Cannot convert () => {} to a BigInt

1

u/_xiphiaz Mar 21 '24

Interesting, though symbols are not objects which probably explains that.

2

u/SoInsightful Mar 21 '24

We can probably find engine-level explanations for all of these cases if we dig deep. I'm just saying that they're not all "reasonable" ❤️

1

u/nommu_moose Mar 21 '24

I would argue that it should explicitly require a conversion to string. It would improve readability to no end, and make clearer the cause of issues if somebody messes that up and passes an unintended type.

It at least makes the mistake deliberate.