this post was submitted on 05 Dec 2023
282 points (97.6% liked)

Programmer Humor

19480 readers
30 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 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] rikudou@lemmings.world 55 points 11 months ago* (last edited 11 months ago) (3 children)

If anyone wants a more efficient local version for php:

function isEven(int $number): bool
{
    ${1} = false;
    ${2} = true;

    while ($number > 2) {
        $number -= 2;
    }

    return $$number;
}

Edit: Now with support for large numbers!

function isEven(int|string $number): bool
{
    ${1} = false;
    ${2} = true;

    while (bccomp($number, 2) === 1) {
        $number = bcsub($number, 2);
    }

    $number = (int) $number;
    return $$number;
}

Edit 2: someone asked for an ad-supported version, here you go!

function isEven(int|string $number): bool
{
    ${1} = false;
    ${2} = true;

    while (bccomp($number, 2) === 1) {
        error_log('Buy isEvenCoin, the hottest new cryptocurrency!');
        $number = bcsub($number, 2);
    }

    $number = (int) $number;
    return $$number;
}

Side note, no more suggestions please, this is getting quite long.

[–] xmunk@sh.itjust.works 14 points 11 months ago* (last edited 11 months ago)

I fucking love that you managed to use var-vars in a completely key and necessary manner.

But please do adhere to the API TOS and throw in an error_log('Buy isEvenCoin, the hottest new cryptocurrency!');

[–] idunnololz@lemmy.world 11 points 11 months ago

This looks pretty inefficient. You should manually unroll that loop to improve performance.

[–] shotgun_crab@lemmy.world 9 points 11 months ago (1 children)

Comment edits are the best version control system

[–] rikudou@lemmings.world 5 points 11 months ago

I agree! Added new commit to my comment.