• lowleveldata
    link
    fedilink
    arrow-up
    42
    arrow-down
    4
    ·
    2 years ago

    It is sort of readable. A switch is “perfectly” readable for switching.

      • Kryomaani@sopuli.xyz
        link
        fedilink
        English
        arrow-up
        19
        arrow-down
        1
        ·
        2 years ago

        Which is exactly why you shouldn’t be using them in a situation that clearly calls for a switch.

        • Serdan@lemm.ee
          link
          fedilink
          English
          arrow-up
          5
          arrow-down
          1
          ·
          edit-2
          2 years ago

          In the given example I’d probably use a switch / match expression, but ternaries are usually more flexible than switches and I don’t think it’s an issue to write a nested ternary instead of if else statements.

          • lowleveldata
            link
            fedilink
            English
            arrow-up
            4
            arrow-down
            2
            ·
            edit-2
            2 years ago

            ternaries are usually more flexible than switches

            Which is bad for readability because the reader need to manually compute it to see whether it’s doing simple switching or not. Also it adds the question of “Why did the author use a nested ternary instead of a switch? Was it meant to do more but it got left out unintentionally?”

            • Serdan@lemm.ee
              link
              fedilink
              English
              arrow-up
              3
              arrow-down
              2
              ·
              2 years ago

              Yes, you need to read code to understand it. If else statements can also do the job of a switch, so the exact same argument applies.

              • lowleveldata
                link
                fedilink
                English
                arrow-up
                3
                arrow-down
                1
                ·
                2 years ago

                The point is I don’t need to read a switch statement to know that it is a switch