• javasux@lemmy.world
    link
    fedilink
    English
    arrow-up
    29
    ·
    edit-2
    14 hours ago

    bruh why is unsucked a global variable

    also that function shouldn’t be named Check if it does things other than checking (e.g. sucking)

    • Programmer Belch@lemmy.dbzer0.com
      link
      fedilink
      English
      arrow-up
      12
      ·
      12 hours ago

      Could be inside a class for something to be sucked, making unsucked a variable for the class. CheckAndSuck would be a better name for the function. I don’t think the meme needed a code audit but here I am.

      • davidagain@lemmy.world
        link
        fedilink
        arrow-up
        4
        ·
        edit-2
        3 hours ago

        Your naming advice is universally good.

        However, if this was a functional programming language, there wouldn’t be any mutable global variables to be unaware were being examined, nor could Suck do any sucking unless it were passed the thing to suck and returned the sucked thing.

        In this way the subtle class of bugs that you both are warning against would be impossible to introduce.

        Depending on the kind of sucking that Suck does, however, you may perceive the global invisibility and availability of the sucking as an advantage in this case. But possibly not if the code is your girlfriend/boyfriend.

        • Programmer Belch@lemmy.dbzer0.com
          link
          fedilink
          English
          arrow-up
          2
          ·
          2 hours ago

          I didn’t need to do this but I did and now I want it in my comment history:

          class Suckable {
              bool unsucked;
          
          public:
              Suckable () {
                  unsucked = true;
              }
          
              void Suck() {
                  unsucked = false;
              }
          
              void CheckAndSuck() {
                  if (unsucked) {
                      Suck();
                  }
              }
          };
          

          Sorry for making you see c++, it’s the language I’m currently using. This program compiles on my machine and doesn’t use global variables.