Switch statement with returns — code correctness

前端 未结 17 2177
梦如初夏
梦如初夏 2020-12-12 20:05

Let\'s say I have code in C with approximately this structure:

switch (something)
{
    case 0:
      return \"blah\";
      break;

    case 1:
    case 4:
         


        
相关标签:
17条回答
  • 2020-12-12 20:49

    I'd normally write the code without them. IMO, dead code tends to indicate sloppiness and/or lack of understanding.

    Of course, I'd also consider something like:

    char const *rets[] = {"blah", "foo", "bar"};
    
    return rets[something];
    

    Edit: even with the edited post, this general idea can work fine:

    char const *rets[] = { "blah", "foo", "bar", "bar", "foo"};
    
    if ((unsigned)something < 5)
        return rets[something]
    return "foobar";
    

    At some point, especially if the input values are sparse (e.g., 1, 100, 1000 and 10000), you want a sparse array instead. You can implement that as either a tree or a map reasonably well (though, of course, a switch still works in this case as well).

    0 讨论(0)
  • 2020-12-12 20:52

    Personally I would remove the returns and keep the breaks. I would use the switch statement to assign a value to a variable. Then return that variable after the switch statement.

    Though this is an arguable point I've always felt that good design and encapsulation means one way in and one way out. It is much easier to guarantee the logic and you don't accidentally miss cleanup code based on the cyclomatic complexity of your function.

    One exception: Returning early is okay if a bad parameter is detected at the beginning of a function--before any resources are acquired.

    0 讨论(0)
  • 2020-12-12 20:53

    If you have "lookup" type of code, you could package the switch-case clause in a method by itself.

    I have a few of these in a "hobby" system I'm developing for fun:

    private int basePerCapitaIncomeRaw(int tl) {
        switch (tl) {
            case 0:     return 7500;
            case 1:     return 7800;
            case 2:     return 8100;
            case 3:     return 8400;
            case 4:     return 9600;
            case 5:     return 13000;
            case 6:     return 19000;
            case 7:     return 25000;
            case 8:     return 31000;
            case 9:     return 43000;
            case 10:    return 67000;
            case 11:    return 97000;
            default:    return 130000;
        }
    }
    

    (Yep. That's GURPS space...)

    I agree with others that you should in most cases avoid more than one return in a method, and I do recognize that this one might have been better implemented as an array or something else. I just found switch-case-return to be a pretty easy match to a lookup table with a 1-1 correlation between input and output, like the above thing (role-playing games are full of them, I am sure they exist in other "businesses" as well) :D

    On the other hand, if the case-clause is more complex, or something happens after the switch-statement, I wouldn't recommend using return in it, but rather set a variable in the switch, end it with a break, and return the value of the variable in the end.

    (On the ... third? hand... you can always refactor a switch into its own method... I doubt it will have an effect on performance, and it wouldn't surprise me if modern compilers could even recognize it as something that could be inlined...)

    0 讨论(0)
  • 2020-12-12 20:54

    What do you think? Is it fine to remove them? Or would you keep them for increased "correctness"?

    It is fine to remove them. Using return is exactly the scenario where break should not be used.

    0 讨论(0)
  • 2020-12-12 20:54

    I say remove them. If your code is so unreadable that you need to stick breaks in there 'to be on the safe side', you should reconsider your coding style :)

    Also I've always prefered not to mix breaks and returns in the switch statement, but rather stick with one of them.

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