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;
}
3
  • 1
    Does this answer your question? Multiple cases in switch statement Commented Dec 3, 2021 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. Commented Dec 3, 2021 at 11:46
  • Protocols are read from Db and there are 5 different but I use only 2 of them for email sending. Commented Dec 3, 2021 at 11:49

3 Answers 3

8

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

Sign up to request clarification or add additional context in comments.

1 Comment

Sometimes when multiple solutions are available I choose the solution most programmers will understand and on that metric your answer wins.
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));

1 Comment

This will throw if key is not present. OP code - does not.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.