When you test your regex and it doesn't work... That's how you know it's broken and you need to debug it.
It's crazy that people don't shit all over other languages that have generic errors like "seg fault".. sometimes people do things so silly that there's no explicit error message. That's not a fault of the language.
When you test your regex and it doesn't work... That's how you know it's broken and you need to debug it.
Dude, I hate "JS bad because I did silly thing" criticisms as much as the next person, but "just fix your regex when you finally notice it's randomly failing for no reason" is not a good defense of the language.
sometimes people do things so silly that there's no explicit error message. That's not a fault of the language.
That's absolutely a fault of the language. What else would it be?
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.
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.
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.
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!
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?
An error is an error. If you expect a certain kind or value of output, but instead you get something else, it's an error. Whether it's valid memory or not.
If you write code that could possibly pass an empty object into your regex constructor, that's an error.. and the fault lies with the programmer who didn't validate.
Passing in an empty object instead of a string is not any more or less of an error than passing in a reference to an empty spot in memory, or just the wrong spot in memory.
I'm trying to figure out how that's relevant to whether or not the programmer should not be passing variables to a function if they haven't verified the appropriate data type first...
Cause you compared it to a segfaults, but segfaults happens for valid reasons that cannot be resolved by the language, this scheiße has exaclty 0 rights to be a thing
So then replace seg fault with literally any other error that developers have complained about not being descriptive or helpful. There are thousands of them..
I write JavaScript. Which is why I'm so dumbfounded that people act like these are real problems... In 8 years I have NEVER had an issue like this occur... Nor any of the issues about what NaN matches or doesn't match...
There are myriad ways of doing stupid things in any language. And none of those are a good reason to shit on a language instead of the dumb developer who tried to do something stupid.
But don't you understand?! Because it can happen it's a problem! I've been coding JS for like 20 years in the before times when there wasn't even XMLHttpRequest and I have never run into this.
I guess long story short... Sanitize your inputs ahead of time instead of relying on error messages to be pre-built-in to the language.
If you have a function that takes a string and passes it to a console log.... A simple if statement to check if it's the correct data type is all you need.
This is how you should handle it even if there was a built in error message.
I could make an equally absurd remark in the opposite direction and it doesn't make you wrong... So why do you think this makes me wrong?
"Why even have programming languages at all? Let's just let coders use ai tools to write perfect bug free code."
It's meaningless to use such absurd examples. We have lots of different programming languages all designed for different things. We need to use those programming languages if we want to write programs. Each language is going to have quirks.
One of JavaScript 's quirks is that it attempts to finish executing no matter what. It prioritizes completion without intrusive errors popping up. That can make for a smoother experience for the user.. but it also means the developer has to be careful.
There are obviously other languages designed for other purposes where the priority was skewed heavily towards calling out every error.
I just have a deep aversion to fanboyism on anything which a lot of the comments here piqued.
I'm not fanboying at all.... I'm pushing back on the absurdly intense criticism that JavaScript gets for the sole reason that it doesn't explicitly tell you when you fucked up... I'm trying to get people to understand that if they're writing code like that.... It's not the language's fault that their debugging sessions are harder than they should be.
If anything you're acting as a fanboy of the "anti-js crowd".
but to pretend that doesn't come with a host of unsavory qualities,
At no point did I pretend js has no unsavory qualities.... I'm just not putting the blame on the language when developers do stupid shit the language wasn't meant to do.
If a car breaks down because the owner never changed the oil.... It's not a problem of the car. If a 747 crashes because the pilot went rogue and decided to try a barrel roll... That's not a fault of the 747.
Anything can break if you use it in a way that wasnt intended.
Even if I wanted everything to execute no matter what -- Why would I not want to warn the user at the very least when I coerce an object to the string 'Object object' ?
Because 99.9% of users would have absolutely no idea what that means. It's not a helpful error message for them. And the problem should reveal itself in testing, not in production.
It's not that JS throws no warnings or errors, so how can we really defend this without some hero worship of the language because it is known to us?
It throws errors when something is bad enough that it literally can't proceed...
Who's hero worshiping? All I've said is that the fault lies with the people doing stupid shit, not the language they're writing it in. I'm not claiming JavaScript is perfect or divine or free of flaws... I'm saying that it has flaws just like all other languages.
I could sit down and come up with quirky things about every major language... And that task wouldn't do anything to actually make those languages bad. It would just prove that I thought of a lot of ways to break them.
View all comments
Show parent comment
3
u/subject_deleted Mar 20 '24
When you test your regex and it doesn't work... That's how you know it's broken and you need to debug it.
It's crazy that people don't shit all over other languages that have generic errors like "seg fault".. sometimes people do things so silly that there's no explicit error message. That's not a fault of the language.