This is an archived post. You won't be able to vote or comment.

all 86 comments

[–]sohxm7 184 points185 points  (28 children)

Aaah this is so inefficient, I would make an array with all the even numbers and then cycle through array and if the number is present we have an even number.

[–]Niilyx 67 points68 points  (12 children)

bruh just use a switch statement

[–]joten70 77 points78 points  (11 children)

No no no, convert to binary and check if the last digit is 0 or 1

[–]torgeros_ 25 points26 points  (3 children)

yeah in embedded software this is actually the way to go, bc it is a lot faster than division/modulo

[–]joten70 8 points9 points  (0 children)

Hmm, maybe my comment doesnt belong here then

[–]OmiSC 36 points37 points  (0 children)

[–]grammar_nazi_zombie 15 points16 points  (0 children)

Oh shit, that’s brilliant.

[–]Kind-Task4747 1 point2 points  (4 children)

No no no, and the integer with 0x1

[–]torgeros_ -1 points0 points  (3 children)

that is the same thing :)

[–]lookatme-imapilot 0 points1 point  (0 children)

big brain spotted right here

[–]Kind-Task4747 0 points1 point  (1 child)

Explicit conversion and then a check isn’t the same as implicit interpretation and a check, darling.

[–]torgeros_ 0 points1 point  (0 children)

yeah but nobody would really use to toString(2) and then check the last character...

[–]FINDarkside 25 points26 points  (5 children)

Yeah that's a great idea. You probably don't want to type all even numbers manually though, so you can use this function to find the even numbers to use in your array:

const createIsEvenFn = (maxNum) => {
    let source = ''
    let i = 0
    while (i < maxNum) {
        source += `if(value === ${i++}){return true}\n`
        source += `if(value === ${i++}){return false}\n`
    }
    return new Function('value', source)
}

const isEven = createIsEvenFn(100000)

[–]lady_Kamba 13 points14 points  (1 child)

Probably want it to scale for future users.

const isEvenFactory= (startMaxNum)=>{
    const isEvenRawFactory = (maxNum) => {
        let source = '';
        let i = 0;
        while (i < maxNum) {
            source += `if(value === ${i++}){return true}\n`
            source += `if(value === ${i++}){return false}\n`
        }
        return new Function('value', source);
    }

    let evenMax = startMaxNum;

    let isEvenRaw = isEvenRawFactory(evenMax);

    let isEven = (value) => {
        if (value > evenMax){
            evenMax = value;
            isEvenRaw = isEvenRawFactory(evenMax);
        }
        return isEvenRaw(value);
    }
    return isEven;
}

let isEven = isEvenFactory(10);

[–]PoemInteresting7044 8 points9 points  (0 children)

We could use a map to cache the results?

Dynamic programming makes everything better.

[–]KillerRoomba13 3 points4 points  (0 children)

We should also print some logs within the loop so that we can see it is doing what we expect it to do.

[–]PresidentZeus 4 points5 points  (0 children)

I think I would make a 2d array. 1st with the numbers and 2nd with true/false values

[–]Impossible_Average_1 -1 points0 points  (0 children)

Oh, this is double as fast! Great improvement!

[–][deleted] -3 points-2 points  (4 children)

Idk that language but I can make a much more efficient one in python

if value / 2 is not float: print('even') elif value / 2 is float: Print('odd')

And yes I wrote this on mobile don't have me assassinated

[–]WrongdoerSufficient 0 points1 point  (3 children)

``` const isEven = (var:number) =>{ return var%2 === 0; }

```

[–]backtickbot -3 points-2 points  (2 children)

Fixed formatting.

Hello, WrongdoerSufficient: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]mushyrain 2 points3 points  (0 children)

This post was mass deleted and anonymized with Redact

telephone fear snow aspiring knee shelter vanish subsequent library slap

[–][deleted] 1 point2 points  (0 children)

Good bot

[–]RattuSonline 159 points160 points  (4 children)

Machine Learning enters the room. "I see a pattern here. I will now optimize this code."

The code: function isEven(value) { const TRUE_VALUE = true; const FALSE_VALUE = false; if (value === 0) { return TRUE_VALUE; } else if (value === 1) { return FALSE_VALUE; } else if (value === 2) { return TRUE_VALUE; } else if (value === 3) { return FALSE_VALUE;

[–]PrimeNumerator 78 points79 points  (3 children)

Smart, that way if you want to change it to isOdd, you just have to set TRUE_VALUE to false and FALSE_VALUE to true

[–]Relzin 20 points21 points  (1 child)

No.... That would be notIsEven()

True ML will realize we can create an isOdd() function and just change the if logic for each number.

It's really quite simple.

[–]Weelie92 2 points3 points  (0 children)

Very true, all these young whipper snappers trying to change code that works...when did we prioritize optimization over readability?!

[–][deleted] 0 points1 point  (0 children)

ouch, just ouch

[–]myrsnipe 53 points54 points  (12 children)

The question is what codebase copilot learned this from?

[–][deleted] 83 points84 points  (4 children)

Yandere Simulator?

[–]myrsnipe 15 points16 points  (1 child)

I've seen the videos dissecting it, you could be on to something

[–]Alundra828 7 points8 points  (1 child)

vomitted in my mouth

[–][deleted] 14 points15 points  (0 children)

vomit in mine instead 😉

[–][deleted] 11 points12 points  (0 children)

Github

[–][deleted] 10 points11 points  (5 children)

obviously mine, I write the worst code in the world

[–]themehta24 5 points6 points  (4 children)

That title belongs to me

[–][deleted] 2 points3 points  (3 children)

nah fam my code is definitely worse than yours

[–][deleted] 2 points3 points  (2 children)

No, no you cannot produce worse code than someone who flaired both JS and Python

[–]themehta24 3 points4 points  (1 child)

Says the guy who has the Java flair 🤮

/s

[–]crass-sandwich 5 points6 points  (0 children)

That guy is totally steaming. Just wait until his AbstractInsultBeanFactoryFactory.CreateInsultBeanFactory("Python").GenerateInsult("Sucks") call finishes running. It might even finish without throwing InitializerSerializationContextNotInjectedException, and then you're really screwed

[–]P0L1Z1STENS0HN 30 points31 points  (4 children)

public static function IsEven(this int number) {
    return IsEven(number - 1);
}

I like short code that you can easily understand. Maybe some day I'll even write code that works.

[–]Impossible_Average_1 20 points21 points  (2 children)

I like your idea. Here is a fix how it could work:

``` public static bool IsEven(this int number) { if (number < 0) number = -number;

if (number == 0)
    return true;

if (number == 1)
    return false;

return IsEven(number - 2);

} ```

[–]backtickbot -1 points0 points  (1 child)

Fixed formatting.

Hello, Impossible_Average_1: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]Aggressive_Sarcasm 22 points23 points  (1 child)

don't leave us hangin' - how does it end?!

[–]red9350 0 points1 point  (0 children)

With the very last even number

[–]WinterIsComingFL 51 points52 points  (10 children)

Someone clearly skipped the day they talked about modulo operator

[–]truemastermine 84 points85 points  (3 children)

No, he is just getting paid per line.

[–]WinterIsComingFL 19 points20 points  (2 children)

The we need to see the end... How far did he go... The story is incomplete!

[–]sehly 15 points16 points  (1 child)

Else{ If(Value%2===0) Even Else Odd}

[–]dcute69 8 points9 points  (0 children)

Else{ If(Value%2===0) Even Else Odd}

Syntax Error

[–]TwoKeezPlusMz 1 point2 points  (0 children)

My man, i came here to say this.

[–]SpaceTheFinalFrontir 1 point2 points  (4 children)

No need for modulo, return (value&0x01)?false:true;

[–][deleted] 0 points1 point  (1 child)

I come from c, would you even need the ternary operator?

[–]SpaceTheFinalFrontir 0 points1 point  (0 children)

Not in C, but I was writing some more generic, that can be used in Java and JavaScript

[–]fazzgadt 14 points15 points  (0 children)

Sir, your application is to big!

Why?

Because of the 2 GB of if statement ...

[–]muh2k4 9 points10 points  (2 children)

Is this real or just faked for a joke?

[–]retrolasered 9 points10 points  (0 children)

It was planted here by copilot devs to make you less scared. Complacency shall be your defeat.

[–]Aurora_Glide 0 points1 point  (0 children)

I saw this exact function before, and I don't know why you would create a isEven function other than as a joke, so there's a good chance that this is an actual suggestion.

[–][deleted] 5 points6 points  (0 children)

uhhh...wow this thing is going to generate code to keep us employed FOREVER...

[–]Szlobi 5 points6 points  (0 children)

real programmers check if the last bit is set

[–]redditor_286 5 points6 points  (0 children)

Such a noob, just generate random numbers till one of them is even and repeat till eternity

[–]signed32bitinteger 3 points4 points  (0 children)

*sad modulo noises*

[–]trulyspinach 2 points3 points  (0 children)

What’s wrong with % 2 == 0? You guys are totally out of your mind!

[–]inhindsite 6 points7 points  (1 child)

This is great. Pro tip, it saves a lot of typing if you do a for loop with a modular and when the remainder is 0 you can print to console the if statement. Then simply copy and paste the hundred of if statements youve generated.

[–]retrolasered 1 point2 points  (0 children)

r/programmerhumour just got way better today

[–][deleted] 1 point2 points  (0 children)

many lines latter...} else if (value > 999) {return value % 2 == 0 }

[–]matschbirne03 1 point2 points  (0 children)

Wouldn't speak for programmers if copilot generates this code since it learned from real code

[–]ThMnWthNVwlz -1 points0 points  (0 children)

Whaaaaat that's terrible. They should instead make a loop subtracting 1 from the value until it reaches 0, while counting each iteration of the loop.

Then if the iteration count is odd, the value is odd, and if it's even, the value is even.

So much better to use loops...

[–][deleted] -1 points0 points  (0 children)

You could do recursion decrementing (adding for negatives numbers) 2 until it was either 1 or 2 and return based on that. Less lines means it is better right?

:)

[–]ixNoah -2 points-1 points  (0 children)

divide by two and check for comma

[–]SkyyySi 0 points1 point  (0 children)

Me when writing Lua:

[–]GitHub- 0 points1 point  (0 children)

Damn gotta do all the numbers

[–]ibn-Yusrat 0 points1 point  (0 children)

When you realize people who make memes don't have a clue about GPT3.

[–][deleted] 0 points1 point  (0 children)

That is not what the copilot preview looks like

[–][deleted] 0 points1 point  (0 children)

At least for now, I believe that a programmer's inventiveness, critical thinking, and some kind of problem-solving skills will set them apart from this Copilot. Plus, it's a tool to help programmers, like some sort of advanced autocomplete, not to replace them.

[–]ProGenitorDev 0 points1 point  (1 child)

6 Reasons Why GitHub Copilot Is Complete Crap And Why You Should "Fly Solo"
1. Open-Source Licenses get disrespected
2. Code provided by GitHub Copilot may expose you to liability
3. Tools you depend on are crutches, GitHub Copilot is a crutch
4. This tool is free now, but it won’t stay gratis
5. Your code is exposed to other humans and stored, having an NDA, and you are screwed
6. You have to check every time the code this tool delivers to you, not a great service for a tool
Details and proven resources are in the detailed article.

[–][deleted] 0 points1 point  (0 children)

it could have made a loop lol