this post was submitted on 13 Dec 2025
651 points (97.7% liked)

Programmer Humor

30000 readers
2207 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 50 comments
sorted by: hot top controversial new old
[–] ArbitraryValue@sh.itjust.works 88 points 2 months ago (1 children)

The advantage of that last approach is that it has side effects and cannot therefore be optimized out by the compiler.

[–] ronigami@lemmy.world 13 points 2 months ago (2 children)

That’s only one advantage. In theory it does not necessarily terminate, so that’s another one.

load more comments (2 replies)
[–] OshaqHennessey@midwest.social 63 points 2 months ago (2 children)
function myFunction() {
  try {
    x = new Random().nextInt();
    if (x != 10) {
     throw "not 10";
    }
    else {
      return (10)
    }
    catch(err) {
      myFunction()
    }
  }
}

x = myFunction()

Commit notes: Added error handling

[–] firewallfail@lemmy.world 62 points 2 months ago (1 children)

Returning 10 instead of x when x finally ends up being 10 really ties it together.

[–] OshaqHennessey@midwest.social 28 points 2 months ago

I'm glad you noticed. That was my favorite part too.

[–] NikkiDimes@lemmy.world 4 points 2 months ago (1 children)

SyntaxError: Unexpected token 'catch'

[–] OshaqHennessey@midwest.social 9 points 2 months ago

Coding on mobile is hard

[–] edinbruh@feddit.it 41 points 2 months ago (1 children)

For a time on Reddit (some years ago when I still used it) there was a trend of finding the worst way of implementing is_even(x: int) -> bool. My contribution to that was a function that ran Ackerman(x,x) flipping a Boolean at every iteration, and check if it was true or false at the end.

It works btw, I will find the proof later

[–] uranibaba@lemmy.world 13 points 2 months ago (1 children)

I would love to see the implementaion.

[–] edinbruh@feddit.it 19 points 2 months ago* (last edited 2 months ago) (6 children)

The implementation is not very exciting, I capture a variable in python. It could have been done more cleanly.

1000041934

The proof is this. But, I could have made mistakes, it was many years ago.

1000041935

Note that in python you'll never be able to run is_even(5) the stack cannot handle it

Edit: daaaamn, that variable is ugly as hell. I would never do things like that now.

load more comments (6 replies)
[–] Hirom@beehaw.org 31 points 2 months ago (2 children)

The compiler will optimize it anyway. /s

[–] Dumhuvud@programming.dev 17 points 2 months ago* (last edited 2 months ago) (1 children)

You jest, but you aren't wrong. At least if we are talking about C, C++ or Rust. https://godbolt.org/z/oPPfdfcf5

.NET compiler is weak when it comes to optimizing your code; I assume Go's is as bad.

[–] yggstyle@lemmy.world 4 points 2 months ago

Technically yes... But I think he was more making the excuse for the gore "from the goresmith's perspective."

And I'm not sure if the compiler in any language would change a random check function... The others are a possibility.

[–] yetAnotherUser@discuss.tchncs.de 6 points 2 months ago (1 children)

Not sure about the last one though. The other two are trivial to optimize away.

[–] Hirom@beehaw.org 3 points 2 months ago* (last edited 2 months ago) (3 children)

An infinite loop canot be ruled out in the last case, so a compiler couldn't optimize this away without potentially changing the program behavior.

load more comments (3 replies)
[–] Grandwolf319@sh.itjust.works 27 points 2 months ago* (last edited 2 months ago) (5 children)

How about

x=x-x

x++

x++

x++

x++

x++

x++

x++

x++

x++

x++

[–] okmko@lemmy.world 8 points 2 months ago* (last edited 2 months ago)

Freshman year of college doing assembly programming, I spent a while figuring out a "programmic" way to solve a problem, trying to wrangle labels and gotos. My friend came in with essentially this but as lookup table. It blew my mind.

It was then that I learned to trade space for complexity.

[–] xthexder@l.sw0.com 7 points 2 months ago (1 children)

This is actually a valid brainf*ck program, but it results in 19, not 10.

[–] Grandwolf319@sh.itjust.works 6 points 2 months ago (2 children)

How so? It’s only 10 increments

[–] anton@lemmy.blahaj.zone 5 points 2 months ago

Because the only brainfuck instructions in your comment where a - which decrements and 20 +, each of which increments.
Mine echos the first two characters from stdin, because of the commas and dots.

load more comments (1 replies)
load more comments (3 replies)
[–] ulterno@programming.dev 25 points 2 months ago (2 children)

That's not even enough to get you a job these days.
You now have to use:

do {
    x = reinterpret_cast<int>(AI::Instance().ask("Do Something. Anything. Be efficient and productive. Use 10 tokens."));
} while (x != 10);
[–] Tetragrade@leminal.space 8 points 2 months ago (2 children)

This isn't just a function, it's a bold restatement of what it means to write code — a symphony of characters, questioning the very nature of the cutting edge language models that I want to beat with hammers.

load more comments (2 replies)
[–] melfie@lemy.lol 6 points 2 months ago (2 children)

You’re absolutely right! Who sets a variable these days without running it though a LLM?

load more comments (2 replies)
[–] Mika@piefed.ca 23 points 2 months ago (1 children)

I once was helping to organize the testing of town-level algorithmic competition for school students.

The competition had one entry level issue that was basically solvable by reading the question properly, recognising that it's just multiplication of two numbers, and writing the simplest app ever.

And there was one student who passed the automatic tests. We had to read the code too for the protocol, just to make sure there was no cheating.

We looked in the code. What? Why? It had two nested for loops and a++ inside. When we understood what's going on we couldn't stop laughing for like solid ten minutes.

[–] TheOakTree@lemmy.zip 3 points 2 months ago

Multiplication is just repeated addition :) glad it worked for the kid, despite the... inefficiency.

[–] baggachipz@sh.itjust.works 15 points 2 months ago (1 children)

It would get you promoted at Twitter, where lines of code is the productivity metric.

load more comments (1 replies)
[–] 6nk06@sh.itjust.works 15 points 2 months ago (4 children)

What is the value of x in the Good example before the loop?

[–] Devial@discuss.online 28 points 2 months ago

Doesn't matter, it's 10 after the loop.

[–] BassTurd@lemmy.world 15 points 2 months ago (1 children)

An int. Value doesn't matter because it's overwritten.

[–] OshaqHennessey@midwest.social 3 points 2 months ago (1 children)

Not in this case. First, i is declared and assigned a value of 0. Next, x is declared and assigned a value of -i or -0. On the first loop iteration, i will decrement to -1, perform the conditional check, then execute the loop body which will assign x to -i or -(-1) or positive 1, and so on.

The only time a variable is created without a value is if you declare one without assigning a value like with

[int]i;

[–] BassTurd@lemmy.world 5 points 2 months ago (4 children)

I know. OP asked what x was before the loop, and I just said it's an int. The int can be any value because as you pointed out it will be set to 0 in the first loop iteration.

load more comments (4 replies)
load more comments (2 replies)
[–] moseschrute@lemmy.world 10 points 2 months ago (1 children)

Probably Microsoft: You’re hired! Go work on GitHub

Context

[–] cooligula@sh.itjust.works 3 points 2 months ago

I'd say Meta hiring someone to work on WhatsApp. Man, is that piece of software crap... Every update, a new UI bug/glitch appears

[–] Atlas_@lemmy.world 9 points 2 months ago (3 children)

Oddly enough, out of all of these the one the compiler has the best chance of optimizing out is the last one

[–] zea_64@lemmy.blahaj.zone 4 points 2 months ago

Not if Random writes to global state, that's a side effect that must be preserved

load more comments (2 replies)
[–] Ooops@feddit.org 6 points 2 months ago (1 children)

Wants to be Pro but doesn't even do it recursive...

[–] OshaqHennessey@midwest.social 5 points 2 months ago* (last edited 2 months ago) (1 children)
function foo() {
  x = new Random();
  case (x = 10):
    return (x);
  default:
    foo()
}
[–] anton@lemmy.blahaj.zone 3 points 2 months ago* (last edited 2 months ago) (1 children)

What unholy mix of languages is that? It is dominated by a blend of javascript and python, but with notes of something exotic. Maybe algol? or vhdl?, there is to little to tell.
Impressive, someone write up a spec and publish it to the esolang wiki.

[–] OshaqHennessey@midwest.social 6 points 2 months ago

It's an incoherent hodgepodge of C#/.NET, PowerShell, and JavaScript, each of which I've forgotten more about than I currently know

[–] OshaqHennessey@midwest.social 4 points 2 months ago

Now write a function to unroll the while loop to "optimize it for the compiler"

[–] irelephant@lemmy.world 4 points 2 months ago
 x = (function(){
return 10
})

Or something like that

[–] ThunderLegend@sh.itjust.works 3 points 2 months ago (3 children)
load more comments (3 replies)
[–] untorquer@lemmy.world 3 points 2 months ago* (last edited 2 months ago)

Something like

int *a = new int(10)

Int*b = null

While *b !=10 { b = rand(); a=new int(10)}

Return *b

I haven't coded recently enough in c/c++ to remember syntax but the concept might work eventually if you're lucky and have enough memory... Might need a time variant seed on the rand()...

[–] phpinjected@lemmy.sdf.org 2 points 2 months ago

bad but more chaotic

you want to be punk rock not blues or jazz

load more comments
view more: next ›