2

I am working with legacy C# code, and am seeing two styles of switch statements. I come from a Python background, so the nuances of the difference between these two switch uses eludes me

Example #1

public string SwitchExample1(int value)
{
    switch(value)
    {
        case 1: return "a";
        case 2: return "b";
        default: return "c";
    }
}

Example #2

public string SwitchExample2(int value)
{
    switch(value)
    {
        case 1: return "a";
        case 2: return "b";
    }

    return "c";
}

Functionally they are the same - they both return "c" when the value != 1 && value != 2

Is this simply a style difference? Or are there best practices that dictate you use one over the other?

dbJones
  • 762
  • 1
  • 10
  • 31
  • It is a programming style issue. Good Programming practices required method to return from last statement of code. The first case isn't following Good Programming Practices. – jdweng Oct 26 '17 at 16:50
  • 1
    Static code analyzers would typically warn, on the second case, that you forgot to add a `default` case (where you could, at the very least, do something like `throw NotSupportedException()`). That alone might be sufficient reason to make it part of the statement, like it logically is. (This makes more sense when you consider that the typical use case is for enums, where the list of values is *apparently* not open-ended, but in actual *practice* is since the values aren't restricted to the defined tags, so forgetting to check invalid values is a common error.) – Jeroen Mostert Oct 26 '17 at 16:52
  • 1
    Depending on the scenario I'd also prefer to have `default` throw an exception. Otherwise your `default` is going to silently accept future values that weren't expected when the `switch` was written. How certain can I be that the handler I write today will be correct for future value that I haven't anticipated? Throwing an exception forces me to address it. – Scott Hannen Oct 26 '17 at 17:06
  • @jdweng The last statement on the first example is the **`switch` statement!** The stuff inside it is part of it's *body*, AKA *block*, AKA, [**compound statement**](https://msdn.microsoft.com/en-us/library/eb685ekd.aspx). --- I have no idea where you've found a *"good programming practice"* guide that tells you to move *the obvious default behavior* out of the `default` case, just for the sake of having it as a separate (last) statement, but I *can* tell you three things: IMHO, 1) the guide is doing it wrong, 2) you're learning it wrong, and worst of all, 3) you're spreading the wrong. – CosmicGiant Oct 26 '17 at 17:18
  • The return is returning from the method not out of the switch statement. In this case the default is doing nothing do not put anything in the default.. This has nothing to do with the return. The return statement is recommended to be the last line of code. – jdweng Oct 26 '17 at 21:30
  • @jdweng If the `return`s within the `switch` statement *are **returning** out of the method*, and all execution `case`s within the `switch` lead to a `return` statement, then it logically follows that the `switch` statement is the last statement in the method. --- Having the default `return` outside of the `switch` just for the sake of having the last method be called `return` rather than a `switch`, ***makes absolutely no sense***. It's like advocating for `void` methods to have a `return;` on the last line, just for the sake of *"having the last statement be a `return`"*. It's nonsense. – CosmicGiant Oct 27 '17 at 14:53

2 Answers2

3

There is no practical difference. Both formats will behave the same as:

if(value==1)
    return "a";
else if(value==2)
    return "b";
else
    return "c";

The first approach, of using default, is strongly recommended. It's the one that will follow style guidelines, and that will make the intent of the code clearest.

CosmicGiant
  • 6,275
  • 5
  • 43
  • 58
-2

Most style guidelines say one should use a 'default' statement.

JosephDoggie
  • 1,514
  • 4
  • 27
  • 57
  • See also https://stackoverflow.com/questions/3110088/switch-statement-must-default-be-the-last-case – JosephDoggie Oct 26 '17 at 16:50
  • Not sure *why* downvoted (as of 10/26/2017), the statement is certainly true.... – JosephDoggie Oct 26 '17 at 16:52
  • 3
    [Do not upvote solely to free the post from an undeserved negative vote.](https://meta.stackexchange.com/questions/104829/is-it-reasonable-to-upvote-in-order-to-counter-what-i-think-is-an-unjustified-do/104832#104832) :P @AlmightyR – Christian Gollhardt Oct 26 '17 at 16:59
  • 2
    @ChristianGollhardt While I didn't declare I had other reasons, I also didn't explicitly declare that "to counter the haters" was the *only* reason, did I? =P --- The answer is valid, AFAICS. --- And ultimately, [what I do with my votes is my decision.](https://meta.stackexchange.com/a/209850/197882) – CosmicGiant Oct 26 '17 at 17:05
  • I would add that most employers take style guidelines very seriously; I have had formatting issues (which don't effect run-time code at all) be brought up -- in a very serious manner. Many shops would *require* a default here ... – JosephDoggie Oct 27 '17 at 12:39