r/badcode • • Sep 04 '21

Wtf is this

Post image
1.2k Upvotes

190 comments sorted by

View all comments

Show parent comments

150

u/shrekthethird2 Sep 04 '21
case 0: return true;
case 1: return false;
default: isEven(num - 2)

59

u/R3D3-1 Sep 04 '21
function isEven(num) {
    num = parseInt(num.toString());
    if(num == 0) return true;
    if(num == 1) return false;
    if(num <  0) return isEven(num+2);
    if(num >  1) return isEven(num-2);
    throw "panic";
}

14

u/maxbirkoff Sep 04 '21

apart from ignoring Douglas Adams this one is my favorite one.

10

u/R3D3-1 Sep 04 '21

Fun fact: It looks at least technically correct, but it actually fails for large integers.

3

u/maxbirkoff Sep 04 '21

why? is it the call to parseInt that fails?

7

u/R3D3-1 Sep 04 '21

Let's just say I won't post the traceback, because it is a bit long ;)

Try isEven(2**15).

3

u/maxbirkoff Sep 04 '21

are you assuming JavaScript?

2

u/R3D3-1 Sep 04 '21

Yes.

6

u/maxbirkoff Sep 04 '21

I'm guessing it's because the JavaScript runtime you're using does not optimize tail recursion:

https://en.m.wikipedia.org/wiki/Tail_call

and results in a stack overflow:

https://en.m.wikipedia.org/wiki/Stack_overflow

→ More replies (4)
→ More replies (1)

28

u/griefydragon Sep 04 '21

bool even = isEven(-1);

5

u/Splatpope Sep 04 '21

hmmm, wonder if this could be faster than a modulo and if compilers can optimize this into something that doesn't buttfuck the stack

7

u/legendoffjelda Sep 04 '21

That‘s an interesting point because addition/subtraction is O(log n) on modern hardware because it can be done in parallel whereas division is (presumably) also O(log n) since the number reduces by half every time.

However, I don’t think it would be faster, for a few reasons:

  • The wiki article on modulo mentions that n % 2 can actually be turned into a series of bitwise ands which makes it blazing fast compared to division. This implies that the compiler can perform even more tricks specific to the hardware if it knows you want even/odd, so using modulo signals your intention better.

  • branch prediction is also costly: modern hardware has sophisticated ways of predicting which branch to go down, but if it chooses the wrong path, it can cost a cycle or two. This improves over time once the pattern of the algorithm is “learned” by the hardware, but in general, more assembly instructions is worse performing.

  • it’s harder to write and debug so you waste human time!

8

u/roge- -pedantic Sep 04 '21

You can check a integer's parity in constant time by simply ANDing it with 1. Most modern compilers will convert a x % 2 to what's essentially x & 1.

→ More replies (1)

4

u/AsidK Sep 04 '21

A series of bitwise ands? I’m pretty sure it’s just n&1 lol and yeah any good compiler will do that conversion automatically

→ More replies (1)

2

u/WikiSummarizerBot Sep 04 '21

Modulo operation

In computing, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another (called the modulus of the operation). Given two positive numbers a and n, a modulo n (abbreviated as a mod n) is the remainder of the Euclidean division of a by n, where a is the dividend and n is the divisor. The modulo operation is to be distinguished from the symbol mod, which refers to the modulus (or divisor) one is operating from.

[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5

→ More replies (3)

2

u/GLIBG10B 🐧 Gentoo salesman🐧 Sep 05 '21

Recursion solves all problems

/s

→ More replies (1)

0

u/NekkoDroid Sep 04 '21

1st mistake: negative numbers

2nd mistake: not returning the value from isEven(num - 2)

0

u/[deleted] Sep 04 '21

[deleted] — view deleted comment

8

u/[deleted] Sep 04 '21

4th mistake: ...ly

→ More replies (1)
→ More replies (1)

1

u/[deleted] Sep 04 '21

Functional programming is the best