this post was submitted on 13 Dec 2025
372 points (97.9% liked)

Programmer Humor

27836 readers
1093 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
all 44 comments
sorted by: hot top controversial new old
[–] ArbitraryValue@sh.itjust.works 59 points 12 hours ago

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

[–] edinbruh@feddit.it 31 points 12 hours 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 7 points 10 hours ago (1 children)

I would love to see the implementaion.

[–] edinbruh@feddit.it 9 points 7 hours ago* (last edited 6 hours ago)

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

collapsed inline media1000041934

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

collapsed inline media1000041935

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.

[–] Hirom@beehaw.org 28 points 13 hours ago (2 children)

The compiler will optimize it anyway. /s

[–] Dumhuvud@programming.dev 15 points 11 hours ago* (last edited 11 hours 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 10 hours 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.

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

[–] OshaqHennessey@midwest.social 28 points 7 hours 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 21 points 6 hours ago (1 children)

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

[–] OshaqHennessey@midwest.social 9 points 6 hours ago

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

[–] NikkiDimes@lemmy.world 1 points 6 hours ago (1 children)

SyntaxError: Unexpected token 'catch'

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

Coding on mobile is hard

[–] Grandwolf319@sh.itjust.works 19 points 10 hours ago* (last edited 10 hours ago) (2 children)

How about

x=x-x

x++

x++

x++

x++

x++

x++

x++

x++

x++

x++

[–] xthexder@l.sw0.com 4 points 10 hours ago (1 children)

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

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

How so? It’s only 10 increments

[–] anton@lemmy.blahaj.zone 4 points 8 hours 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.

[–] juliebean@lemmy.zip 1 points 8 hours ago

it's been a long time since i looked at brainfuck, but i suspect that '+' denotes an increment, and '-' denotes a decrement, so we've got one decrement and 20 increments.

[–] okmko@lemmy.world 4 points 7 hours ago* (last edited 7 hours 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.

[–] Mika@piefed.ca 15 points 8 hours ago

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.

[–] 6nk06@sh.itjust.works 13 points 13 hours ago (4 children)

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

[–] Devial@discuss.online 27 points 12 hours ago

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

[–] BassTurd@lemmy.world 14 points 12 hours ago (1 children)

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

[–] OshaqHennessey@midwest.social 3 points 12 hours 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 4 points 11 hours ago (1 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.

[–] OshaqHennessey@midwest.social 1 points 7 hours ago (1 children)

Shit, you're right. x is declared inside the loop, so it doesn't exist until the loop begins execution.

Technically, I suppose you could say the compiler will allocate memory for x without assigning a value before the loop is executed and... I'm understanding what you mean now, I think.

[–] anton@lemmy.blahaj.zone 1 points 7 hours ago (2 children)

The code seems to be C-style language with curly braces and types in front for variable declarations, probably java. This means the variable must be declared of screen before the loop or it would not compile. It could have a previous value or be uninitialized, but that does not affect the end result.

[–] BassTurd@lemmy.world 1 points 6 hours ago

I read in on C but it's also true for JavaScript. The code implies that x was declared as an int sometime previously, or if JavaScript, just an object if not assigned a value giving it a type.

[–] OshaqHennessey@midwest.social 1 points 6 hours ago

Yeah, it does look like C now that I think about it. You're right about the end result too. I believe C# will let you do inline declaration and assignment like that, so maybe that's what we're looking at? Been a while, could be wrong

[–] OshaqHennessey@midwest.social 1 points 12 hours ago

If this is JavaScript, it would have a value of -0, which is actually valid and works the same as normal zero thanks to type coercion. I think the only difference is some methods that detect if a number is negative will return true instead of false, but otherwise, JS treats -0 the same way as 0

[–] Ooops@feddit.org 6 points 9 hours ago (1 children)

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

[–] OshaqHennessey@midwest.social 4 points 7 hours ago* (last edited 7 hours ago) (1 children)
function foo() {
  x = new Random();
  case (x = 10):
    return (x);
  default:
    foo()
}
[–] anton@lemmy.blahaj.zone 2 points 6 hours ago* (last edited 6 hours 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 2 points 6 hours ago

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

[–] Atlas_@lemmy.world 4 points 2 hours ago (1 children)

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

[–] OshaqHennessey@midwest.social 4 points 12 hours ago

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

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

bad but more chaotic

you want to be punk rock not blues or jazz

[–] spongebue@lemmy.world 1 points 7 hours ago (4 children)

x = -i;

Do many languages let you do that? When it's in front of a variable I would've expected it to be a subtraction operator only and you would need to do x = -1 * i;

[–] EvilHankVenture@lemmy.world 4 points 6 hours ago

In most languages I've seen - is both a unary negation operator and a subtraction operator depending on context. So it would negate an integer literal or a variable in this context.

[–] boonhet@sopuli.xyz 2 points 6 hours ago

Personally I would expect it to behave the same in front of a numeric literal and in front of a variable. I do think most languages do that, but I haven't actually tested that many and could br wrong.

[–] squaresinger@lemmy.world 1 points 8 minutes ago

Why would they not let you do that? I honestly don't know a single language that wouldn't let you do that. Same as basic math notation allows you to do that.

x = -i

is a totally valid mathematical equation.

[–] OshaqHennessey@midwest.social 1 points 6 hours ago

I just tested it in PowerShell. Works fine

$i = 1
$x = -$i
$x

Outputs -1