r/ProgrammerHumor • • Mar 20 '24

Meme areJSDevsActuallySane

Post image
14k Upvotes

554 comments sorted by

View all comments

Show parent comment

1

u/subject_deleted Mar 21 '24

but "just fix your regex when you finally notice it's randomly failing for no reason" is not a good defense of the language.

That's not what I said though....

I said when you write your regex... You should test it. Just like you would for any language...

Also, we're not talking about the regex "failing for no reason". There is a reason... You're passing an empty object into the regex constructor.. that's the reason it's failing.

That's absolutely a fault of the language. What else would it be?

It would be the fault of someone who did something silly.. like passing the output of a function straight to a regex constructor without validating the output of that function first... If you do that and get an unexpected result because you didn't validate first... That's not on the language.

1

u/SoInsightful Mar 21 '24

The issue is obviously not that someone would sit down and write new RegExp({}) and wonder why their regex doesn't work as expected.

It will be a project with thousands and files and even more thousands of dependencies, and at some time, somewhere, a value happened to result in a non-string which 58 stack frames later was passed to a new RegExp constructor. And then suddenly some cog in the system only works some X% of the time.

To be frank, your idea of "a JavaScript function shouldn't throw because you should simply load your entire system into your brain and write everything perfectly" reeks of inexperience.

1

u/subject_deleted Mar 21 '24

The issue is obviously not that someone would sit down and write new RegExp({}) and wonder why their regex doesn't work as expected.

I didn't say that that was the problem.... I said that when you write a new regex,.you should test it. That includes throwing stuff at it that you expect it to receive.... But it also means throwing stuff at it you don't expect it to receive.

If part of your test passes an empty object to your regex, you'll see right away that it's not doing what is expected. Then you KNOW you need to avoid passing an object to that function and you can put a simple conditional statement to throw an error if the argument was an object.

To be frank, your idea of "a JavaScript function shouldn't throw because you should simply load your entire system into your brain and write everything perfectly" reeks of inexperience.

I never said any of that... What is the value of attributing words to me that I never said? What does that accomplish? What do you gain?

I didn't say write everything perfectly. I said sanitize your inputs so that when a function is called and it needs a string as an argument.... Just put a condition that checks if the argument is a string. If this seems like impossibly complex stuff to you, you might be the inexperienced programmer.

1

u/SoInsightful Mar 21 '24

Just put a condition that checks if the argument is a string.

Oh, so something like this?

if (typeof input !== 'string') {
  throw new TypeError('RegExp input must be a string');
}

That's actually a great idea! If only the language did exactly that for me so I wouldn't have to track down obscure bugs if one of my colleagues or library authors accidentally forgot to do that!

1

u/subject_deleted Mar 21 '24

If only the language just checked the strings you wanted it to check without having to call a function....

If only the script could just automatically figure out whether a number was even or odd without me having to write a function to check it..

If only the language automatically grabbed input from the user so we didn't have to bother making forms and processing the inputs.

If only, right?

Essentially what you're doing here is identical to arguing that c is a bad language because you have to indicate what type a variable is when you declare it... It's an unnecessary step for you, because other languages have features built in that automatically determines what the data type is.

Do you think all languages should be interpreted? If not, why not? If not, why would you point to things that a language COULD do automatically and then shit on that language for not doing it automatically?