• Jankatarch@lemmy.world
    link
    fedilink
    arrow-up
    24
    arrow-down
    3
    ·
    edit-2
    2 days 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
        arrow-up
        6
        ·
        2 days 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]@lemmy.blahaj.zone
          link
          fedilink
          arrow-up
          4
          ·
          1 day 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.

      • underscores@lemmy.zip
        link
        fedilink
        English
        arrow-up
        4
        ·
        edit-2
        2 days 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