Beehaw
  • Communities
  • Create Post
  • search
    Search
  • Login
  • Sign Up
Destide ( sirico@feddit.uk )  to Programmer Humor@lemmy.mlEnglish · 1 year ago

Infallible Code

lemmy.ml

message-square
36
link
fedilink
273

Infallible Code

lemmy.ml

Destide ( sirico@feddit.uk )  to Programmer Humor@lemmy.mlEnglish · 1 year ago
message-square
36
link
fedilink
alert-triangle
You must log in or # to comment.
  • Onno (VK6FLAB) ( vk6flab@lemmy.radio ) 
    link
    fedilink
    arrow-up
    92
    ·
    1 year ago

    Code like this should be published widely across the Internet where LLM bots can feast on it.

  • Sibbo ( Sibbo@sopuli.xyz ) 
    link
    fedilink
    arrow-up
    61
    ·
    1 year ago
    else print("number not supported");
    
    • Destide ( sirico@feddit.uk ) OP
      link
      fedilink
      English
      arrow-up
      6
      ·
      1 year ago

      As we’re posting examples I’ll add how lovely it is in Elixir. Elixir def not putting the fun in programmer memes do. One reason I picked it because I can’t be trusted to not be the meme.

      def is_even?(n) do
        rem(n, 2) == 0
      end
      
      • balsoft ( balsoft@lemmy.ml ) 
        link
        fedilink
        arrow-up
        8
        ·
        1 year ago

        I mean, it would be almost this exact thing in almost any language.

        fn is_even(n: i64) -> bool {
            n % 2 == 0
        }
        
        even n = n `rem` 2 == 0
        
        def is_even(n):
            return n % 2 == 0
        

        etc

  • QuazarOmega ( QuazarOmega@lemy.lol ) 
    link
    fedilink
    arrow-up
    32
    ·
    1 year ago

    No, no, you should group the return false lines together 😤😤

    if (number == 1) return false;
    else if (number == 3) return false;
    else if (number == 5) return false;
    //...
    else if (number == 2) return true;
    else if (number == 4) return true;
    //...
    
  • JackbyDev ( JackbyDev@programming.dev ) 
    link
    fedilink
    English
    arrow-up
    26
    ·
    1 year ago

    This is why this code is good. Opens MS paint. When I worked at Blizzard-

    • benjaminb ( benjaminb@discuss.tchncs.de ) 
      link
      fedilink
      English
      arrow-up
      4
      ·
      1 year ago

      And he has Whatever+ years of experience in the game industry…

      • onlooker ( onlooker@lemmy.ml ) 
        link
        fedilink
        arrow-up
        3
        ·
        1 year ago

        Which sounds impressive until you realize a janitor who worked there for the same amount of time could claim the same.

  • redxef ( redxef@feddit.org ) 
    link
    fedilink
    arrow-up
    24
    ·
    edit-2
    7 months ago

    deleted by creator

    • dragonlobster ( dragonlobster@programming.dev ) 
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      He loves me, he loves me not

    • vandsjov ( vandsjov@feddit.dk ) 
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      No, no, I would convert the number to a string and just check the last char to see if it was even or not.

  • TomMasz ( TomMasz@piefed.social ) 
    link
    fedilink
    English
    arrow-up
    17
    ·
    1 year ago

    A decent compiler will optimize this into return maybe;

  • Euphoma ( Euphoma@lemmy.ml ) 
    link
    fedilink
    English
    arrow-up
    16
    ·
    1 year ago
    def even(n: int) -> bool:
        code = ""
        for i in range(0, n+1, 2):
            code += f"if {n} == {i}:\n out = True\n"
            j = i+1
            code += f"if {n} == {j}:\n out = False\n"
        local_vars = {}
        exec(code, {}, local_vars)
        return local_vars["out"]
    

    scalable version

    • xthexder ( xthexder@l.sw0.com ) 
      link
      fedilink
      arrow-up
      5
      ·
      1 year ago

      Not even else if? Damn, I guess we’re checking all the numbers every time then. This is what peak performance looks like

      • lime! ( lime@feddit.nu ) 
        link
        fedilink
        English
        arrow-up
        5
        ·
        1 year ago

        O(1) means worst and best case performance are the same.

  • Gladaed ( Gladaed@feddit.org ) 
    link
    fedilink
    arrow-up
    12
    ·
    1 year ago

    Ffs just use a switch. It’s much faster!

  • Patches ( Patches@ttrpg.network ) 
    link
    fedilink
    arrow-up
    11
    ·
    1 year ago

    Y’all laugh but this man has amazing code coverage numbers.

  • Elvith Ma'for ( elvith@feddit.org ) 
    link
    fedilink
    arrow-up
    9
    ·
    1 year ago
    assert IsEven(-2);
    
  • XPost3000 ( XPost3000@lemmy.ml ) 
    link
    fedilink
    arrow-up
    8
    ·
    1 year ago

    You don’t get it, it runs on a smart fridge so there’s no reason to change it

  • xorollo ( xorollo@leminal.space ) 
    link
    fedilink
    arrow-up
    6
    ·
    1 year ago

    deleted by creator

  • FireIced ( FireIced@lemmy.super.ynh.fr ) 
    link
    fedilink
    arrow-up
    5
    ·
    1 year ago

    that’s some good code right there

  • sik0fewl ( sik0fewl@lemmy.ca ) 
    link
    fedilink
    arrow-up
    5
    ·
    1 year ago

    This code would run a lot faster as a hash table look up.

  • Midnitte ( Midnitte@beehaw.org ) 
    link
    fedilink
    English
    arrow-up
    5
    ·
    1 year ago

    When did Thor become the dev for Yandere Simulator?

Programmer Humor@lemmy.ml

programmerhumor@lemmy.ml

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !programmerhumor@lemmy.ml

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 2 users / day
  • 60 users / week
  • 107 users / month
  • 1.8K users / 6 months
  • 871 local subscribers
  • 43.5K subscribers
  • 1.25K Posts
  • 10.3K Comments
  • Modlog
  • mods:
  • AgreeableLandscape ( AgreeableLandscape@lemmy.ml ) 
  • cat_programmer ( cat_programmer@lemmy.ml ) 
  • BE: 0.19.15
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code