Are multiple conditional operators in this situation a good idea?

前端 未结 21 2754
名媛妹妹
名媛妹妹 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:01

    The conditional operator version is clean, simple, and it's immediately obvious to anybody who knows C or C++ what is happening. Its other virtue is that it returns a value immediately, which means it can be put in the initialization (like this example is).

    A switch statement would be more clumsy. It would require that the variable be declared and then initialized, usually a bad idea if it can be avoided. It would take more typing, and would have more places for bugs to creep in. It wouldn't be as clear, since it would be necessary to look at each case to see that it said something like new_vehicle = foo; break;.

    If you're going to do this look up here only, then having the conditional version right there is good, as it immediately shows what's happening. If it's going to occur more than once, consider putting it in a function, so that there's only one place to update if anything changes (such as, say, 'R' for carriage or 'L' for helicopter).

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

    I think its useful for someone who code it, but will be difficult to understand for the reviwer,

    "KEEP IT SIMPLE BUDDY"

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

    I would use a switch, because this is the kind of thing it was designed for. Yes, there is a risk of fall-through bugs, but this nested-conditional block has a much higher risk of being misunderstood by other programmers.

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

    In my opinion what you've done is acceptable due to the simplicity of the example. If you were doing more things with each case this type of construct could rapidly get messy. For that reason I would prefer a switch or even nested if then elses (if there aren't too many cases), formatted as follows:

      if (A) {
          //Do A stuff
      }
      else if (B) {
          //Do B stuff
      }
      else if (C) {
          //Do C stuff
      }
      else {
          //Do default stuff
      }
    

    It's about the readability of the code, which lends itself to the maintainability of the code. I've never been a huge fan fan of the conditional operator, because I don't like to see multiple expressions on a single line. Conditional operators can be difficult to follow when single stepping the code in a debugger. The simpler the code the easier it is to concentrate on what the code is doing.

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

    Many of us are used to the Iif-functions in various reporting tools or the If-function in Excel where we basically need to use the less clear Iif(arg="B";"Bus";Iif(arg="A";Airplane;"Feet")). I love your sample compared to that :) I personally would've used if-elses, but I would not have a problem with your sample.

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

    There's a lot of whitespace around the character constants that makes it a bit hard to read. I'd parenthesize the comparisons: (and maybe move the last value in line.)

    Vehicle new_vehicle = (arg == 'B') ? bus      :
                          (arg == 'A') ? airplane :
                          (arg == 'T') ? train    :
                          (arg == 'C') ? car      :
                          (arg == 'H') ? horse    :
                                         feet;
    

    Now it looks great.

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