Why do most programming languages only have binary equality comparison operators?

前端 未结 24 1016
一个人的身影
一个人的身影 2020-12-08 14:20

In natural languages, we would say \"some color is a primary color if the color is red, blue, or yellow.\"

In every programming language I\'ve seen, that translates

24条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 14:37

    In Python you can say ...

    isPrimaryColor = someColor in ('Red', 'Blue', 'Yellow')
    

    ... which I find more readable than your (== "Red" or == "Blue") syntax. There's a few reasons to add syntax support for a language feature:

    • Efficiency: Not a reason here, since there's no speed improvement.
    • Functionality: Also not a concern; there's nothing you can do in the new syntax that you can't do in the old.
    • Legibility: Most languages handle the case where you're checking the equality of multiple values just fine. In other cases (e.g., someNumber (> 1 and < 10)) it might be more useful, but even then it doesn't buy you much (and Python allows you to say 1 < someNumber < 10, which is even clearer).

    So it's not clear the proposed change is particularly helpful.

提交回复
热议问题