Are multiple conditional operators in this situation a good idea?

前端 未结 21 2757
名媛妹妹
名媛妹妹 2020-12-15 06:23

I just saw this block of code on the Wikipedia article on conditional operators:

Vehicle new_vehicle = arg == \'B\' ? bus      :
                      arg ==         


        
相关标签:
21条回答
  • 2020-12-15 07:24

    How about:

    enum Vehicle { bus = 'B', airplane = 'A', train, car = 'C', horse = 'H', feet = 'F' };
    ...
    new_vehicle = arg;
    

    :-), by the way.

    0 讨论(0)
  • 2020-12-15 07:25

    I don't particularly care for it.

    It doesn't really buy anything, or make anything more clear, and it's a pretty non-standard usage of the operator.

    It seems the primary advantage is that it's somewhat clever. I avoid clever unless there's a pretty good (external) reason to be clever.

    0 讨论(0)
  • 2020-12-15 07:26

    I would lean toward a switch statement because the compiler will catch duplicate cases. Arguably, this is not an issue in this example but if the list gets really long and is worked on by several different people it is easy to add a duplicate and not realize it.

    0 讨论(0)
提交回复
热议问题