0

Can I somehow simplify this switch statement as both cases do same thing just with another function parameter?

switch (data.Subscriber.Protocol)
{
    case "email json":
        builder.Attachments.Add("Očitanje.json", CraftAttachment(data));
        break;
    case "email text":
        builder.Attachments.Add("Očitanje.txt", CraftAttachment(data));
        break;
    default:
        break;
}
mohabbati
  • 1,162
  • 1
  • 13
  • 31
forrestDumb
  • 57
  • 1
  • 7
  • 1
    Does this answer your question? [Multiple cases in switch statement](https://stackoverflow.com/questions/68578/multiple-cases-in-switch-statement) – d4zed Dec 03 '21 at 11:45
  • You say "both cases", but you should really consider `default` to be a separate case. So there are 3. Unless you're saying that "email json" and "email text" are the only possible values, which we have no way of knowing. – Damien_The_Unbeliever Dec 03 '21 at 11:46
  • Protocols are read from Db and there are 5 different but I use only 2 of them for email sending. – forrestDumb Dec 03 '21 at 11:49

3 Answers3

6

How about something like this:

string attachmentName = data.Subscriber.Protocol switch
{
    "email json" => "Očitanje.json",
    "email text" => "Očitanje.txt",
    _ => null
};

if (attachmentName is not null)
{
    builder.Attachments.Add(attachmentName, CraftAttachment(data));
}

Switch expression | C# reference

Richard Deeming
  • 29,830
  • 10
  • 79
  • 151
  • Sometimes when multiple solutions are available I choose the solution most programmers will understand and on that metric your answer wins. – camelCase Dec 09 '21 at 19:28
0

//Another clean approach without using Swtich case:

var ProtocolAndFileMappings = new Dictionary<string, string>()
    {
       {"email json","Očitanje.json"},
       {"email text","Očitanje.json"},
       {"email png","Očitanje.png"},
       {"email Jpeg","Očitanje.Jpeg"}
     };

 builder.Attachments.Add(ProtocolAndFileMappings[data.Subscriber.Protocol], CraftAttachment(data));
Mysterious Jack
  • 621
  • 6
  • 18
0

Another approach using a local function to simplify the call:

void add(string s) => if (s != null) builder.Attachments.Add(s, CraftAttachment(data));

add( data.Subscriber.Protocol switch
{
    "email json" => "Očitanje.json",
    "email text" => "Očitanje.txt",
    _ => null
});

(Although I think that some folks would criticize that as "too cute...)

NOTE: This solution (like the other solutions) suffers from a drawback.

The code will always make an additional test against null, which the straightforward switch doesn't do - so this is (very marginally) less efficient.

I personally would do it like this (which avoids the drawback):

void add(string s) => builder.Attachments.Add(s, CraftAttachment(data));

switch (data.Subscriber.Protocol)
{
    case "email json": add("Očitanje.json"); break;
    case "email text": add("Očitanje.txt")   break;
    default:           /* Do nothing */      break;
}    

Simple local functions like that are very likely to be inlined by the JIT compiler, so there should be no overhead.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276