277
Infallible Code (feddit.uk)
you are viewing a single comment's thread
view the rest of the comments
[-] sus@programming.dev 87 points 4 days ago* (last edited 4 days ago)

After working at blizzard for 51 years, I finally found an elegant solution by using the power of recursion

private bool IsEven(int number){
  if (number > 1) return IsEven(number - 2);
  if (number == 0) return true;
  if (number == 1) return false;
}
[-] elvith@feddit.org 41 points 4 days ago
[-] lambda@programming.dev 13 points 3 days ago

Now see, you need the other method. IsNegativeEven()

[-] sus@programming.dev 9 points 3 days ago

We can avoid expensive branches (gasp) by using some bitwise arithmetic to achieve the so-called "absolute value", an advanced hacker technique I learnt at Blizzard. Also unlike c, c# is not enlightened enough to understand that my code is perfect so it complains about "not all code paths returning a value".

private bool IsEven(int number)
{
    number *= 1 - 2*(int)(((uint)number & 2147483648) >> 31);
    if (number > 1) return IsEven(number - 2);
    if (number == 0) return true;
    if (number == 1) return false;
    throw new Exception();
}
[-] UnknownSoul@programming.dev 2 points 2 days ago

Nice, now we just need another function to find odd numbers, cause not all numbers are even you know.

[-] Ebber@lemmings.world 13 points 4 days ago

I removed the tail recursion for you:

private book IsEven(int number) {
    if(number > 1) return IsEven(number - 2) == true;
    if(number == 0) return true; 
    if(number == 2) return false;
}
[-] Ferk@programming.dev 3 points 2 days ago

What'd be the result for IsEven(1)?

[-] Ebber@lemmings.world 5 points 2 days ago

Stack overflow

[-] ulterno@programming.dev 3 points 4 days ago

I didn't get this.

Why return book? Does that have some Blizzard reference?
And why would number == 2return false? This is a function for getting true when the number is even, right?

[-] Ebber@lemmings.world 12 points 4 days ago* (last edited 4 days ago)

Haha, you're right. I've now learned two things:

  1. I should not write code on a mobile
  2. I should not become a proof reader

At the end of the day i just wanted the function to be worse, by causing stack overflows

this post was submitted on 15 Jul 2025
277 points (94.2% liked)

Programmer Humor

25098 readers
1712 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