• @Jankatarch@lemmy.world
      link
      fedilink
      22
      edit-2
      4 months ago

      There are two types of programmers.

      // comment
      if(condition) {
        // comment1
        if(condition1) {
          // comment2
          if(condition2) {
            printf("hello, world\\n");
          }
        }
      }
      

      and

      // comment
      if(!condition) {
        return;
      }
      
      // comment1
      if(!condition1) {
        return;
      }
      
      // comment2
      if(!condition2) {
        return;
      }
      
      printf("hello, world\\n");
      

      And one is objectively correct.

        • @kernelle@0d.gs
          link
          fedilink
          74 months ago

          The problem with this in the OP is the first ‘if’ checks if the object exists and the second gets a property of said object only if the original object exists.

          I’m not saying the OP is good code, but chaining them like this would result in exceptions.

          • Fushuan [he/him]
            link
            fedilink
            54 months ago

            The language is python and it has short circuiting aka in an and condition, if the first block isn’t fulfilled the second one isn’t tested because it’s unnecessary.

            Same with or and the reverse.

      • I Cast Fist
        link
        fedilink
        94 months ago
        // comment
        if(x < 10) {
          // comment1
          if(x < 20) {
            // comment2
            if(x < 30) {
              printf("hello, world\\n");
            }
          }
        }
        
        • @underscores@lemmy.zip
          link
          fedilink
          English
          5
          edit-2
          4 months ago

          This is the cursed case when you case the forbidden scroll of the ancients: switch (true) { }

          edit: on second thought I’m not sure now I’ll have to think about how fall through cases work

      • VeganPizza69 Ⓥ
        link
        fedilink
        34 months ago

        Add the else branches to the nested version and log the failed conditions (to make it more obvious).

    • @Tja@programming.dev
      link
      fedilink
      3
      edit-2
      4 months ago

      Not only nested ifs. It’s not even correct (doesn’t check for activity existing). And it’s not even pythonic (ask for forgiveness, not for permission). Just access the thing, catch the exception and be done with it.

    • @CannedYeet@lemmy.world
      link
      fedilink
      -1
      edit-2
      4 months ago

      I worked with chatgpt since I’m not a python dev, and this is what I came up with

      from time import time
      
      class PlaySession:
          def __init__(self, data: dict):
              self.guild_id = int(data['guild_id'])
              self.user_id = int(data['user_id'])
              self.timestamp = data['time']
      
          def is_longer_than_half_hour(self) -> bool:
              return self.timestamp + 1800 < time()
      
          async def resolve_member(self, bot) -> "discord.Member | None":
              guild = bot.get_guild(self.guild_id)
              return guild.get_member(self.user_id) if guild else None
      
          @staticmethod
          def is_playing_league(member) -> bool:
              activity = getattr(member, 'activity', None)
              name = getattr(activity, 'name', None)
              return name and name.lower() == "league of legends"
      
      async def ban_for_league(member):
          await member.send("The 30 minutes has elapsed and you are still playing league, get banned.")
          await member.ban(delete_message_days=0, reason="playing league")
      
      async def process_entries(bot, entry_dicts):
          sessions = [PlaySession(d) for d in entry_dicts if PlaySession(d).is_longer_than_half_hour()]
          for session in sessions:
              member = await session.resolve_member(bot)
              if member and PlaySession.is_playing_league(member):
                  await ban_for_league(member)
      
    • @REDACTED@infosec.pub
      link
      fedilink
      English
      -74 months ago

      Correct me if I’m wrong, but the script works faster with nested IFs since if it fails at the first one, it won’t bother reading next ones, unless you kill/return from function.

      All in all, this feels like readibility argument, not correctness or efficienty argument