this post was submitted on 28 Dec 2023
339 points (97.5% liked)

Programmer Humor

19817 readers
196 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 2 years ago
MODERATORS
top 47 comments
sorted by: hot top controversial new old
[–] [email protected] 76 points 1 year ago (2 children)

I like this bit at the end:

As a side note, the program is amazingly performant. For small numbers the results are instantaneous and for the large number close to the 2^32 limit the result is still returned in around 10 seconds.

[–] [email protected] 13 points 1 year ago (1 children)

Really makes you question your sanity when optimizing jumps in code without benchmarks.

[–] [email protected] 15 points 1 year ago (2 children)

For a long time I've been of the opinion that you should only ever optimize for the next ~~sucker~~ colleague who might need to read and edit your code. If you ever optimize for speed, it needs to be done with massive benchmarking / profiling support to ensure that the changes you make are worth it. This is especially true with modern compilers / interpreters that try to use clever techniques to optimize your code either on the fly, or before making the executable.

[–] [email protected] 7 points 1 year ago* (last edited 1 year ago)

The first rule of optimization: Don’t do it
The second rule of optimization: Don’t do it yet (experts only)

[–] [email protected] 1 points 1 year ago (1 children)

I'm absolutely on-board ...in application code.

I do feel like it's good, though, when libraries optimize. Ideally, they don't have much else to do than one thing really well anyways.

And with how many libraries modern applications pull in, you do eventually notice whether you're in the Python ecosystem, where most libraries don't care, or in the Rust ecosystem, where many libraries definitely overdo it. Because well, they also kind of don't overdo it, since as a user of the library, you don't see any of it, except the culmulative performance benefits.

[–] [email protected] 1 points 1 year ago

Libraries are also written and maintained by humans.

It's fine to optimize if you can truly justify it, but that's going to be even harder in libraries that are going to be used on multiple different architectures, etc.

[–] [email protected] 8 points 1 year ago* (last edited 1 year ago) (1 children)

I'm still mad he didn't use the size of the number to tell the system which block to read first. I feel like that would be a great use of division or maybe modulus?

[–] [email protected] 7 points 1 year ago

I just like how he used "% 2" in the Python code he used to generate the C++ code.

[–] [email protected] 56 points 1 year ago (1 children)

Now we just need to someone to package it and upload it to NPM.

[–] ArtVandelay 7 points 1 year ago* (last edited 1 year ago)

What's another 40 gb of node_modules anyway

[–] [email protected] 39 points 1 year ago (2 children)

Could be easily made 50% space saving by only iffin all odds and return even on else. Maybe one if before to handle overflow to avoid wrong even if over the last if.

[–] [email protected] 38 points 1 year ago

Well yeah, if you allow cheating!

[–] [email protected] 18 points 1 year ago* (last edited 1 year ago) (1 children)

Yeah but then ALL even numbers would be slow to compute because you would have to chain through every odd before you know that 2 is even.

Depends on the expected distribution of input values

[–] [email protected] 1 points 1 year ago

Heuristic: keep it until 512, afterwards powers of 2, and numbers like 1000, 2000,.., 10000, 20000,... (regex: [0-9]000+)

[–] [email protected] 32 points 1 year ago (1 children)

Let's be real though, everything is IF statements all the way down

[–] [email protected] 26 points 1 year ago (3 children)

There's not a single thing in this universe that cannot be accomplished with enough IF statements... as long as you've got infinite time to wait

[–] waz 15 points 1 year ago* (last edited 1 year ago) (1 children)

...you mean IF you've got infinite time to wait?

[–] [email protected] 4 points 1 year ago

...you mean if you've got IFinite time to wait?

[–] [email protected] 2 points 1 year ago (1 children)

The problem with if is the answer comes from user. There’s no mathematical reason or scientific explanation, only programmer who thinks the answer should include the subject.

[–] [email protected] 2 points 1 year ago

True...

But even on a more metaphorical level, every single thing that has or will happen in this universe, down to even the smallest quantum fluctuations could be encapsulated into IF statements as long as you had enough of them.

[–] idunnololz 2 points 1 year ago

What if there was an unintentional infinite loop in your code. You could be waiting for infinite time only to learn the code had a bug. D:

[–] [email protected] 30 points 1 year ago

This is poetry.

My favourite part is that he uses the modulo operator in his Python script to generate the C code.

[–] [email protected] 13 points 1 year ago* (last edited 1 year ago) (1 children)

@programming_horror , anyone? We have an in production version of this used in Wikipedia

[–] [email protected] 5 points 1 year ago (2 children)
[–] [email protected] 2 points 1 year ago
[–] [email protected] 1 points 1 year ago

I'm pretty sure that my link also works regardless of instance. It works when I visit it on your instance.

[–] [email protected] 11 points 1 year ago (1 children)

I'm not a good reader - I skim most articles and often miss most of the meaning. I read, and enjoyed, every word of that!

[–] [email protected] 2 points 1 year ago

Thanks, I totally would've skipped it without this comment.

[–] [email protected] 10 points 1 year ago

Andreas is a maniac

[–] themusicman 9 points 1 year ago (1 children)

It's not a trade off between dev time, execution time and memory as the author claims. It's materially worse for all 3

[–] rainerloeten 20 points 1 year ago* (last edited 1 year ago) (1 children)

I think he was being sarcastic, playing with words. Meaning, that you trade in time, runtime and memory and get nothing in return :D so a pretty bad trade haha.

Of course it's worse, I mean, that was the point of this blogpost, wasn't it? :p It's just a (long) joke.

[–] themusicman 6 points 1 year ago

On reread, you're totally right. Went right over my head

[–] [email protected] 7 points 1 year ago

Well i hate this:

PS > .\program.exe 0

THIS:

.\

[–] Clbull 6 points 1 year ago

This is peak YandereDev.

[–] [email protected] 4 points 1 year ago

I've lost some of my sanity reading this !

[–] dylanTheDeveloper 3 points 1 year ago

It works don't it

[–] [email protected] 2 points 1 year ago (2 children)

This is why every programmer needs to understand the modulo operator.

[–] jaybone 3 points 1 year ago (1 children)
[–] [email protected] 2 points 1 year ago

This is what I prefer too! I also some times prefer to use bitshift when it comes to division or multiplication of power of 2.

[–] [email protected] 2 points 1 year ago (1 children)

I would divide by two (floating point) and check the fractional part.

[–] [email protected] 6 points 1 year ago (1 children)

turns out that 2^53 + 1 is an even number

[–] [email protected] 2 points 1 year ago

The article only covers unsigned 32-bit numbers, so floating point division would be fine.

[–] [email protected] 2 points 1 year ago

Those are rookie numbers. Professionals came up with the nested logic monstrosity that is the JSON-LD specification:

https://www.w3.org/TR/json-ld11-api/#context-processing-algorithm

Looks through the algorithm bits in the various sections. How would you implement that? The answer is invariably by copying the highly nested statements of the spec directly into your language. Maybe there's a better way, but you'd have to understand all that nested logic first, and you'd be exhausted at that point and just want to move on.

[–] [email protected] 1 points 1 year ago

I first saw this joke back in the days of 8-bit home microcomputers. Of course then it only needed 256 lines of code, and took up about 8k of your precious, precious RAM.

[–] [email protected] -1 points 1 year ago (2 children)

I honestly thought this was going to be about AI 😅

CC BY-NC-SA 4.0

[–] [email protected] 3 points 1 year ago

No, this is about NS. Natural Stupidity.

[–] qwertychomp 0 points 1 year ago

Wait, what's with the creative commons license? Are you licensing your comments?