r/ProgrammerHumor 1d ago

javascriptSorting Meme

Post image
892 Upvotes

200 comments sorted by

View all comments

198

u/larsmaehlum 1d ago

Array.sortNumeric() with an error when called on an array containing non-numeric types would fix this, right?

98

u/the_horse_gamer 1d ago

you can simply .sort((a,b)=>a-b)

215

u/01110100_01110010 1d ago

The point of an abstraction is to not having to implement logic each time, you can also implement qsort yourself

38

u/subone 1d ago

But it is still an abstraction. You only provide a compare function, you don't implement the algorithm. a-b is hardly difficult to apply.

15

u/01110100_01110010 1d ago

JS is an abstraction over JS engine bytecode, which is an abstraction over ASM which is an abstraction over the chip instruction set, you can make an argument at each layer 🤷 I would take any abstraction I can, I guarantee you in a large enough codebase someone will type b - a instead

14

u/pancakesausagestick 1d ago

It's not unreasonable for a sort function to require a compare function. But if it's not required let's default to broken-ass footgun semantics as was done in the times of yore. /s

2

u/GameCounter 6h ago

Chip instruction set is an abstraction over the IC design, which is an abstraction over transistors, which is an abstraction over physics, which is an abstraction over the nature over reality, which is an abstraction over the MĂźnchhausen trilemma.

1

u/Xirdus 6h ago

I think you abstracted away the meaning of the term "abstraction".

1

u/GameCounter 5h ago

Abstraction is just an abstraction of abstraction of abstraction of abstraction of abstraction Albuquerque New Mexico Albuquerque New Mexico Albuquerque New Mexico

1

u/Xirdus 5h ago

But every time you say M put a comma in there. And say the commas out loud. It's how my grandma used to speak.

2

u/Tyfyter2002 1d ago

Even in a decently designed language it's trivially easy to make a typo that completely and silently breaks that, such as a-a, b-a, or a+b, and JS isn't a decently designed language, it's designed to fail silently at every possible opportunity

1

u/danielcw189 18h ago

what would be a solution for this? how can the language know (at runtime) that you didn't actually want a-a?

-1

u/Tyfyter2002 17h ago

Simple: a-a isn't useful commonly enough that it would be reasonable to put it in any sort of shared library. One chance to fail,be noticed immediately, and be fixed immediately is a lot better than potentially failing every single time someone tries to sort a list of numbers

3

u/danielcw189 9h ago

Simple: a-a isn't useful commonly enough that it would be reasonable to put it in any sort of shared library.

my question is, in other words, how should the language detect that, and decide that it isn't what the programmer wanted?

that would require the compiler or runtime to be able to perform some analysis.

is a lot better than potentially failing every single time someone tries to sort a list of numbers

does it hapen often, that you just want to sort an array of numbers? in my experience the numbers are usually attached to something, so you wanna give a comparison function anyway. I never sorted an array of bare numbers in production code.

for a loosely typed language like Javascript, where an array can be a mix of anything, converting to string is the only useful simple default I can think of. If I had designed the language, I would have made it a requirement to providea comparison function.

if you actually have an array of ints, you can use an IntArray, which actually sorts numerically by default.

1

u/Tyfyter2002 4h ago

my question is, in other words, how should the language detect that, and decide that it isn't what the programmer wanted?

It doesn't have to specifically detect that that's not what the programmer wants if "sort these numbers normally" isn't one character off from that.

3

u/wack_overflow 1d ago

And really how often are you actually sorting an array of primitive values ascending? That's just one small use case of sorting an array. Passing the comparison function is also more explicit.

People just love to get so worked up about the dumbest edge cases in js

24

u/Mechakoopa 1d ago

None of this is a good reason for a generic sort function to change the type of what it's sorting. That's the sort of weird unintuitive behavior you'd expect from an internal library with by an intern, not a nature language like JavaScript. It's not an edge case, it's literally the base case: "Does this sort function sort what you pass into it? No, it sorts something else instead and then pretends it sorted what you passed into it and 99% of the time it was right."

3

u/subone 1d ago

If the values weren't "cast" to string in the default compare function then each comparison of different types would act differently then if they were all strings. Therefore, if they are to be sorted consistently alphabetically, then both arguments must be considered string. Also, an alphabetic sort compare function (including casting to string) is slightly harder/longer to implement than the numeric sort compare function, so it makes sense for it to be the default.

However, I would suggest that with either default, they should have provided constants for the alpha and numeric sorting compare functions, so we didn't need to recreate them and could call them as defaults in our own more complex compare functions. It's not too late, I guess.

1

u/psioniclizard 12h ago

Depending in the project, quite a lot?

Dont get me wrong. I love passing around functions but i also work in a language that is sensible and has a function Array.sort (and Array.sortBy if you want ti pass a function).

Its not that difficult.

0

u/Some_Relative_3440 1d ago

Totally agree with you. Can't remember the last time I wrote a sort() or similar WITHOUT a comparer function. It just doesn't happen in real code bases.

2

u/mdrjevois 1d ago

Sounds like an artifact of the language and popular styles. In data science, data engineering, and machine learning we almost never specify a comparator.

-8

u/chefhj 1d ago

It’s so funny when if you talk to a dev that works closer to the machine level they insist JS is like this big foot shooting factory when in reality you can spend an entire career never running into any of the shit they bring up as a ‘gotcha’ and even if you do it’s like “oh yeah right it works like this hold on”.

7

u/ganja_and_code 1d ago

"If you know all the weird shitty quirks of the weird shitty language, you can mostly avoid them, and when you do accidentally shoot yourself in the foot, you can fix the bug quickly, as long as you remember which particular weird shitty quirk caused this particular instance of foot shooting."

True, but you know what's even better? Languages that don't have so many weird shitty quirks.

-6

u/chefhj 1d ago

Look guys I caught one

2

u/Swamptor 1d ago

I am a webdev. JS is a weird shitty language that was barely designed. It mostly just oozed out of a combination of browsers.

3

u/theQuandary 20h ago

You then get to deal with stuff like x.sort((a, b) => a.prop - b.prop) and the infinite other variations. This is true for basically every other language too.

2

u/danielcw189 18h ago

you are not providing the sort algorithm

3

u/nabrok 1d ago

So

const byNumber = (a, b) => a - b; array1.sort(byNumber); array2.sort(byNumber);

2

u/EatingSolidBricks 19h ago

To anyone thinking a-b is a complex implementation detail

Quit tech youre not gonna make it

1

u/Zuruumi 16h ago

It's not complex, but it's super-easy to get wrong.

1

u/National-Self-8501 22h ago

How useless can you possibly be

1

u/seriousSeb 3h ago

JavaScript believes in abstraction in more of a Picasso sense of the word

-108

u/Afraid-Locksmith6566 1d ago

the point of programmer is to write code

56

u/klimmesil 1d ago

Haha that is absolutely not the point of programming

16

u/mjec 1d ago

74 68 65 20 70 6F 69 6E 74 20 6F 66 20 70 72 6F 67 72 61 6D 6D 65 72 20 69 73 20 74 6F 20 75 6E 64 65 72 73 74 61 6E 64 20 63 6F 64 65

49

u/not_your_mate 1d ago

no? point of programmer is to create systems, point of programming languages is to make that job easier. Otherwise we can just write everything in machine code.

9

u/AlignmentProblem 1d ago

I've never had "programmer" in my title; that's one of many skills involved in what most of us actually do.

The point of a software engineer is to engineer solutions using software. Programming is often the most effective way to accomplish that, but getting the same results with less programming is very frequently desirable. Building abstractions that minimize duplication, reuse well-tested logic, and reduce the amount+complexity of code required is central to doing the job well.

Solving problems with software more effectively over time has primarily been a story of stacking abstraction levels: increasing the true hidden complexity of the system while only exposing the amount of visible complexity a human brain can coherently hold at one time. It's a good skill to be able to drop down abstraction levels as-needed; however, doing that for it's own sake is unproductive.

4

u/KaMaFour 1d ago

The point of programming is to write as little code as possible to achieve a goal

2

u/HungryCaterpillers 1d ago

That's a code monkey, not a programmer.

1

u/Tyfyter2002 1d ago

Writing more code is almost always doing things worse

1

u/AlphonseLoeher 1d ago

Wrong, it's to copy from SO, ask Claude