• juliebean@lemm.ee
    link
    fedilink
    English
    arrow-up
    0
    ·
    18 days ago

    what would be the alternative? to always execute if the condition is true, but sometimes execute it even when false, for funsies?

    • Admiral Patrick@dubvee.org
      link
      fedilink
      English
      arrow-up
      0
      ·
      edit-2
      18 days ago

      Hey, I like a little chaos in my codebase 😆

      let funsies = (Math.round(Math.random() * 1000) % 2 == 0)
      if (condition || funsies) {
      // do the thing
      }
      
    • bstix@feddit.dk
      link
      fedilink
      English
      arrow-up
      0
      ·
      18 days ago

      The “only” part implies exclusivity, which may be false, because other things might run the code anyway.

      IF “I can see the sun” THEN “It’s day.”

      Nothing wrong about that. However if we make it exclusive:

      IF AND ONLY IF “I can see the sun” THEN “It’s day.”

      That’s obviously wrong. I can actually not keep the day away by sitting with closed eyes in my mothers basement with the curtains shut.

      “Only if” might make sense in a legal contract, but there’s no way a piece of code can stop other pieces of code from calling the same functions.

      • adj16@lemmy.world
        link
        fedilink
        English
        arrow-up
        0
        ·
        16 days ago

        But that’s not how if statements in code work. So what you’ve said isn’t wrong, but the premise of this meme is completely off

        • bstix@feddit.dk
          link
          fedilink
          English
          arrow-up
          0
          ·
          edit-2
          16 days ago

          Yes sure. Code is logical stepwise. By including the “only if” it implies that other stuff is taken into account, which it isn’t at that moment in code.

          I mean, I don’t need to extend the implications of an IF statement. It already does exactly what it says.

          Anyway fuzzy logic does exist for people who want “sometimes if”. It’s useful in certain cases. I’ve only ever considered it in music production, where things very often get to the point of complexity where it makes a (sometimes) useful difference.

    • General_Effort@lemmy.world
      link
      fedilink
      English
      arrow-up
      0
      ·
      edit-2
      17 days ago

      In normal parlance, “if and only if” rules out that something could also happen as a result of other circumstances. EG, if you fall out of a plane, you will lose your glasses. But there are other conditions that would lead to the same result.

      In code, the alternative would be to have a different if statement that executes identical code. Or *cough* you could use a jump instruction to execute literally the same code.