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

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”

🤷‍♂️ 

41

u/user0015 Jun 13 '25

That's the horror.

3

u/Katterton Jun 13 '25

You just need to know a few things about the event loop and how types and references get handled in JS, it's pretty different to most other programming languages, but if you know how it works under the hood it's one of the most intuitive languages out there

5

u/Affectionate-Slice70 Jun 15 '25

intuitive (adjective)

  1. Easily understood or grasped without the need for conscious reasoning.  Example: She had an intuitive sense of direction.
  2. Based on what feels to be true without evidence or reasoning.  Example: His decision seemed intuitive rather than logical.

Origin: From Late Latin intuitivus, meaning "to look at, consider."


→ More replies (5)

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

3

u/random_numbers_81638 Jun 13 '25

I propose throwing an error on trying to use JavaScript

2

u/degaart Jun 14 '25

I propose bricking your UEFI firmware on trying to write javascript

9

u/xezo360hye Jun 12 '25

Holy shit why JS-tards always insist on comparing cars with blue?

4

u/clericc-- Jun 12 '25

deternine the nearest common supertype, which is comparable. thats what should happen. In this case "number".

16

u/Lithl Jun 12 '25

Even ignoring the fact that you're suggesting adding an unnecessary O(n) computation to the sort function, the "nearest supertype" of almost any pair of values of different types is going to be Object.

What is the logical ordering of two arbitrary Objects?

9

u/clericc-- Jun 12 '25

should be "type error: not comparable" of course

5

u/Lithl Jun 12 '25

You're the one suggesting casting the two elements to the nearest common supertype.

7

u/clericc-- Jun 12 '25

and sometimes no common supertype is comparable

→ More replies (1)

2

u/Davvos11 Jun 12 '25

How would you propose to determine that? Keep in mind that the array can have an arbitrarily long length and you would have to do this every time you sort it.

17

u/clericc-- Jun 12 '25

i recommend using statically typed languages and move those determinations to compile time

→ More replies (2)
→ More replies (3)
→ More replies (3)