• RonSijm@programming.dev
    link
    fedilink
    arrow-up
    59
    ·
    7 days ago

    You don’t get it. This was made in GameMaker Studio 1.4, which doesn’t support a modulo operator. You know nothing about this specific framework. I have 8 years of experience and hacked governments. There’s no reason to update it now, because it runs on a smart fridge at maximum capacity.

  • sus@programming.dev
    link
    fedilink
    arrow-up
    48
    ·
    edit-2
    7 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;
    }
    
  • kubica@fedia.io
    link
    fedilink
    arrow-up
    45
    ·
    7 days ago

    You could save about half the code by only listing one boolean value and having the other as a default return at the bottom.

    Sometimes my genius is almost frightening.

    • prime_number_314159@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      6 days ago

      If small numbers are much more frequent, it’s better to return early. Really, you should gather statistics about the numbers the function is called with, and put the most frequent ones at the top.

    • ExLisperA
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      7 days ago

      So to see what’s returned for 3 for example you would have to check the entire function to see if it’s not matched somewhere below and that if actually gets to the default return. This way the first time it matches 3 you can stop reading. It’s has better maintainability.

  • Bytemeister@lemmy.world
    link
    fedilink
    English
    arrow-up
    9
    ·
    6 days ago

    Even plus even equals even.

    Odd plus odd equals even.

    Only odd plus even makes an odd.

    Are there twice as many even numbers as there are odd numbers?

  • magic_lobster_party@fedia.io
    link
    fedilink
    arrow-up
    14
    ·
    7 days ago

    At least this madness is isolated to this function. It can easily be fixed.

    Pirate’s code is just cluttered with magic numbers everywhere. Hard coded numbers that are referring to a big ”story array”, or characters. It’s just a giant web of complexity. The only fix is to start from scratch.

  • harbard@fedia.io
    link
    fedilink
    arrow-up
    6
    ·
    7 days ago

    I pretty much did this at my first coding job lol I was building an online menu that you flip through with the keys lol

    • justOnePersistentKbinPlease@fedia.io
      link
      fedilink
      arrow-up
      5
      ·
      7 days ago

      I did his “optimized” shading trick for a game my friends and I built for a coding project in school.

      Because the project was due in three hours and we realized that we didn’t have any menus. So it was a matter of getting anything functional as menus(stage selection, button mapping, main menu.)
      So the game itself capped at 60fps on the potato school computers, but the static menus only got 20fps.

  • kolorafa@lemmy.world
    link
    fedilink
    arrow-up
    6
    ·
    7 days ago

    Better/fastest approch would be to check the last bit of the int and return the result. Second use modulo.

    This? Dev should burn in hell. Who created this?

    • TheFogan@programming.dev
      link
      fedilink
      English
      arrow-up
      3
      ·
      edit-2
      7 days ago

      or another stupid, but viable way to do it,

      if number = 0:

      return true

      runloop = true

      while runloop:

      if number > 0:

      number -= 2

      else:

      number += 2

      if number = 1:

      return false

      runloop = false

      if number = 2:

      return true

      runloop = false

      still very shitty amature coding, doesn’t depend on modulos, or anything that I can think of that some languages might lack an equivelant of.

      • themoonisacheese@sh.itjust.works
        link
        fedilink
        arrow-up
        3
        ·
        6 days ago

        Okay so basically this is saving bytes on a technicality but also good programming language design (for this specific purpose).

        The first aspect is that since you’re scored on bytes, it’s not really to your advantage to use a language that uses ascii (or utf-8) for it’s tokens, because a large part of it is unprintables like DEL or BELL. So people have designed specially crafted golfing programming languages that use a full 256 possible characters in order to pack as many features as possible in as few bytes as possible.

        The good design part of it is that if you really think about it hard, there’s really not that many things you expect a programming language to do. It turns out that 256 total different operands is about in the sweet spot, so each character that’s available in the 1-byte code page is mapped to one command, and the languages are also designed to make as many things as possible implicit, both at the cost of readability. Remember, all that matters here is getting the lowest score, not code maintainability or anything else.

        This leads to languages like japt (which is a terse form of JavaScript, I’m pretty sure) or pyth (same for python) or Vyxal (my personal favorite, used to be python based but is now bespoke) that look like this but absolutely own at getting a task out in as few bytes as possible.

    • _cnt0@sh.itjust.works
      link
      fedilink
      arrow-up
      2
      ·
      7 days ago

      Though, obviously I had to come up with some ridiculous solutions:

      bool IsEven(int i) => ((Func<string, bool>)(s => s[^1] == 48))($"{i:B}");
      

      This one works without conditionals :)

      bool IsEven(int i)
      {
          try
          {
              int _ = (i & 1) / (i & 1);
          }
          catch (Exception)
          {
              return true;
          }
      
          return false;
      }