r/ProgrammerHumor • • Mar 20 '24

Meme areJSDevsActuallySane

Post image
14k Upvotes

554 comments sorted by

View all comments

Show parent comment

719

u/_Foy Mar 20 '24

That's what's really wild about this whole thing

318

u/Antrikshy Mar 20 '24

The wildest part is that new RegExp doesn't fail when given an object.

172

u/Some-Guy-Online Mar 20 '24

I can excuse a lot of things in JS because of its origin and the efforts to maintain backward compatibility.

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

It should either be a string already or a regex literal.

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.

→ More replies (1)
→ More replies (20)

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!"

→ More replies (1)
→ More replies (7)

9

u/TheLuminary Mar 20 '24

But you are forgetting that is Javascript the language doing it, not `new RegExp` doing that.

One could argue that `new Regexp` should maybe just check the type of the input and error if it is not the expected type, but thats likely expecting too much from Javascript Programmers.

16

u/Some-Guy-Online Mar 20 '24

RegExp is a built-in, so it is in fact JavaScript doing it.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

And yes, the argument is that library functions should validate input types.

I would understand if there is some explanation for why this happens, but it's not readily apparent.

→ More replies (4)

9

u/Antrikshy Mar 20 '24

One could argue that `new Regexp` should maybe just check the type of the input and error if it is not the expected type,

This is what they were saying. Type checking in JS is a thing.

2

u/elehisie Mar 20 '24

Hey at least we’re consistent xD we tell everything will be typecast, everything gets typecasted. Everything lol

→ More replies (8)

10

u/Embarrassed_Ad5387 Mar 20 '24

this, I tried java and can see the boons of not having dynamic types

43

u/[deleted] Mar 20 '24

The issue is type coercion, really. I don't personally like dynamic types for other reasons (mainly poorer linting due to unknown types), but this type of shit doesn't happen in, for example, Python.

→ More replies (12)

8

u/PM_ME_C_CODE Mar 20 '24

This isn't a problem with dynamic typing being "too lose" or anything like that.

This is purely an example of JavaScript being an utterly shit language.

→ More replies (2)

9

u/bronkula Mar 20 '24

The whole point of a loosely typed language is that types don't fail, they convert. That's the whole fucking point.

20

u/Eclipsan Mar 20 '24

Which leads to unexpected behaviors, side effects and bugs. Type juggling is bad.

5

u/edgmnt_net Mar 21 '24

And somehow they think it's faster to write code that way. Until 80% of your time becomes effort spent debugging and writing meaningless tests to exercise every possible thing, because everything can blow up.

→ More replies (1)

8

u/Antrikshy Mar 20 '24

I guess so.

But since RegExp is a class, and not a primitive, it doesn't seem impossible for its constructor to check something like typeof param === "string".

→ More replies (2)
→ More replies (3)
→ More replies (3)