r/programminghorror • • Jun 12 '25 • This 1

Javascript Javascript is filled with horror

Post image
2.4k Upvotes

337 comments sorted by

View all comments

Show parent comment

31

u/Drasern Jun 12 '25

Javascript is built on the foundational concept of continuing execution whenever possible. Things do not throw exceptions unless it is absolutely necessary, you just assume some default functionality and keep going. In this case, there is no way to know what kind of objects are in the array, so it makes more sense to coerce everything to a string than coerce everything to a number. After all, someone might try to sort [1, 2, "a", "17", {prop: "value"}]

11

u/LutimoDancer3459 Jun 12 '25

But is this really the way we want it to handle things? Best case, nothing happens. Worst case, we work with wrong, invalid data that may be persisted and used later on for other stuff.

A coworker once did such a thing. Just use some random chosen value to keep the program from crashing. Resulted in many errors down the line and endless hours wasted of debugging why that is so.

A program is supposed to do what I tell it to do. Not just assume some arbitrary solution just to keep running. The language used should help me get the program I want. Not hiding my incompetence.

-3

u/Randolpho Jun 12 '25

Javascript does it that way because browsers do it that way, and be thankful that choice was made, or else no web page online would render because none of them adhere to the standard.

15

u/LutimoDancer3459 Jun 12 '25

If it were like that, they would be forced to stick to the standard. Nothing bad

-2

u/Randolpho Jun 12 '25

You don’t remember the strict xhtml 4 days, I take it.

8

u/LutimoDancer3459 Jun 12 '25

I am a java developer. I use/ed jsf. Which uses xhtml. So sure. And I prefer it. I dont know why I should expect the browser to render something useful if I missed adding a closing tag.

Most language shout at you if you miss the closing bracket or semicolon... why should i expect the browser languages to behave differently?

Never encounter any endpoint that accepts an malformed json/rest/or whatever format you want to use...

3

u/dopefish86 Jun 12 '25

Does Safari still throw an exception when you try to use localStorage in private mode?

I hated it for that!

2

u/Bobebobbob Jun 12 '25

Javascript is built on the foundational concept of continuing execution whenever possible. Things do not throw exceptions unless it is absolutely necessary

Why in the world would you want that? Catching bugs early is like... possibly the most important part of PL design

4

u/jaaval Jun 12 '25

Javascript philosophy of always continuing execution originates from its roots in writing scripts for interactive web pages. Back in Netscape era. Basically stuff that you really didn't want to crash the page or even the browser but it wasn't so catastrophic if they sometimes did something slightly weird.

Then because there were so many javascript developers available people started to push it everywhere where that philosophy made no sense.