this post was submitted on 25 Oct 2026
364 points (95.0% liked)

Programmer Humor

33809 readers
1962 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 3 years ago
MODERATORS
 

Mine is that the useState hook is an actual plague on the entire React ecosystem. Regardless of your opinions of React, if you wanna use something then use it right.

you are viewing a single comment's thread
view the rest of the comments
[โ€“] stingpie@lemmy.world 1 points 4 hours ago* (last edited 4 hours ago) (1 children)

This is gonna make me sound crazy, but always pass functions when possible, and use case statements for how the passed-in functions are applied.

As a simple example, imagine you want to take the average of a set of numbers. Instead of doing sum(numbers)/len(numbers), do:

Quotient_reduce(sum, lambda x,y: x+1, numbers)

then, inside the definition of quotient_reduce,

Def quotient_reduce(accumulate_by, normalize_by, list):

If accumulate_by==sum and normalize_by==(lambda x,y:x+1):

  return [some efficient implementation of average]

Else:

  Return reduce(accumulate_by, list)/reduce(normalize_by, list)

I know it must look insane, but it generalizes normalization, mean, geometric mean, etc, and on the outside there's no taste of the horrible concrete implementation!

Apologies about formatting, I'm on my phone.

[โ€“] lapping6596@lemmy.world 1 points 2 hours ago

Okay, I do think this makes you sound crazy.

I'm putting my question first before I rant about this cause I think I got way more into this response than is warranted.

What problem does this design of just creating bespoke functions have that your design solves?

def mean(numbers):
    return efficient calculation
def normalization(numbers):
    return efficient calculation
def geometric_mean(numbers):
    return efficient calculation

Rant starts here:

With your design, every time someone wants an average of a set of numbers, you're reimplementing the actual logic. Then if someone forgets the +1 and writes:

quotient_reduce(sum, lambda x,y: x, numbers)

A bug is introduced that's hard to spot in a review, won't give an error, and is hard to write tests for. Turning the lambda to an enum meant to be passed around would solve a some of the accidental bugs, but keeps the interaction with the function more complicated.

I really just don't understand why you wouldn't just write each efficient implementation as it's own function and call it directly.