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

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)

8

u/ricocotam Jun 12 '25

Python handles this very well

5

u/[deleted] Jun 12 '25

Mfw JS cannot guess the CORRECT SORTING MECHANISM FOR FUCKING INTEGERS

→ More replies (2)

18

u/rover_G Jun 12 '25

It makes sense if you’re trying to make a default sorting algorithm that works on untyped arrays

23

u/mediocrobot Jun 12 '25

Sorting untyped arrays is still a wiiiild use case. I know the philosophy behind JS at the time was minimizing exception handling by pretending everything's okay, but this is still kinda ridiculous.

→ More replies (3)