← Back to context

Comment by noelwelsh

14 hours ago

Exactly. I don't understand why people think the ternary operator is needed when you can just make `if` an expression instead of a statement. Then there is no new syntax to learn and `if` just becomes more useful.

I don't really understand where is this need to compress the logic into where small chunks comes from. In result we get single line of code which has multiple statement conditions, different paths, and it's not possible to grasp in one go.

Other practical example why ternary is bad: Many code-coverage solutions break on ternary because they don't correctly see that one of the branches was missed in tests.

  • Because you can deduplicate certain parts of the logic which make the whole thing less error prone, such as

        if c
          x=1
        else
          x=2
    

    If I ever want to change x, or refactor this code some other way, its a more brittle process over x=c?1:2

    The ternary expression also takes up much less space so there is less of an emphasis on it, this can be a stylistic tool in a programmer's toolbox