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

419

u/LordFokas Jun 12 '25

This is a theme. When people shit on JS, it's usually about shit that:

1 - rarely happens / is on you (array sort)
2 - never happens ( [ ] + { } )
3 - is not JS's fault (IEEE-754)

354

u/iamakorndawg Jun 12 '25

I agree with you on 2 and 3, but having the default sort be lexicographic makes absolutely no sense.

105

u/Lithl Jun 12 '25

JavaScript arrays can be any type and even mixed types. What would you propose as the default comparison instead?

107

u/XtremeGoose Jun 12 '25

Exactly what python does. Use the native comparison for those types and if they aren't the same type, throw an error.

128

u/ings0c Jun 12 '25

JS seems to take the philosophy of “what the developer is asking seems very strange but I must never complain. It’s better to just do something seemingly random so their app can silently fail”

🤷‍♂️ 

→ More replies (7)

84

u/floriandotorg Jun 12 '25

Make the comparator mandatory.

In practice you never use ‘toSorted’ without it anyway.

27

u/RegularBubble2637 Jun 12 '25

You do, when you want to sort lexicographically.

15

u/AsIAm Jun 12 '25

Well, then rather use a locale-aware comparator.

8

u/floriandotorg Jun 12 '25

Even then, I would do that, to make explicitly clear what’s happening.

44

u/wyldstallionesquire Jun 12 '25
In [4]: sorted([1,2,3,10])
Out[4]: [1, 2, 3, 10]
In [5]: sorted(["1",2,"3",10])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[5], line 1
----> 1 sorted(["1",2,"3",10])

TypeError: '<' not supported between instances of 'int' and 'str'

6

u/JohnBish Jun 12 '25

Comedy gold

18

u/themrdemonized Jun 12 '25

I propose throwing error on trying sorting mixed type array

→ More replies (2)
→ More replies (17)

47

u/Randolpho Jun 12 '25

What else is it supposed to do? You should have passed in a comparison function, but noooo you had to pass it nothing and make it guess what you wanted it to do.

38

u/Einar__ Jun 12 '25

It would have made more sense if it just required you to pass a comparison function and threw an exception if you didn't. I know it will never happen because backwards compatibility, but everyone can dream.

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.

→ More replies (4)

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!

→ More replies (2)

20

u/the_horse_gamer Jun 12 '25

you CAN pass a comparison function. and since js is all about minimising exceptions, this is a somewhat reasonable default

6

u/PineappleHairy4325 Jun 12 '25

Why is JS all about minimizing exceptions?

21

u/the_horse_gamer Jun 12 '25

a website displaying information slightly wrong is better than a website that doesn't work. that's the core philosophy.

→ More replies (2)
→ More replies (12)
→ More replies (4)
→ More replies (5)

7

u/Apprehensive_Room742 Jun 12 '25

dont know about you, but i sort arrays quite often in my work. also i think its legit to shit on a function implemented by the language that doesn't work. thats just poor design by the people working on javascript

→ More replies (5)

12

u/[deleted] Jun 12 '25

Nah.

  • Garbage collection
  • JIT
  • how much people use strings all over, what is up with that.

You web people can do what you want, but if you stuff JS into applications or games, like some people insist on, then we are not friends.

→ More replies (1)

6

u/Arshiaa001 Jun 12 '25

it's usually about shit that:

1 - rarely happens / is on you (array sort)
2 - never happens ( [ ] + { } )

Until you deserialize some JSON and forget to validate one edge case, and your number is now an empty object. Then all hell breaks loose on production on a Saturday night.

→ More replies (6)
→ More replies (37)