Is it better to use overloading or add an "if" with the optional parameter?

Asked

Viewed 70 times

0

I have a method in which an attribute may not be used

In this case if the value passed is not empty it adds the parameter to my email

public async Task<bool> SendEmail(string to, string subject, string body,string unsubscribeHeaderUrl = "")
{ 
    using (System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage())
    {
        if (!String.IsNullOrEmpty(unsubscribeHeaderUrl)) mail.Headers.Add("List-Unsubscribe",unsubscribeHeaderUrl);
    }
}

But I can do it using the Overloading doing so:

public async Task<bool> SendEmail(string to, string subject, string body,string unsubscribeHeaderUrl = "")
{ 
    using (System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage())
    {
        mail.Headers.Add("List-Unsubscribe",unsubscribeHeaderUrl);
        //Restante do processo
    }
}

public async Task<bool> SendEmail(string to, string subject, string body)
{ 
    using (System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage())
    {
        //Restante do processo
    }
}

What is the right way to do it? Use a if or overloading

2 answers

2


It depends, as always. in most cases the simplest. For this case it seems to me that the first case is better because it is simpler, I always prefer the simplest, including because it is naturally DRY which is the most important principle of programming.

If thinking in terms of performance should not change much and the second can be worse depending on how you do, despite having a branch and this has a higher cost is small and only in cases of needing extreme performance is it worth the effort, but the time it will spend in the method MailMessage() is so big it makes no difference to have it over.

The first will only cost more if you repeat the same code in both methods, which may violate the DRY and cause possible future maintenance problems. One solution to this would be to create a utility method to call you and not repeat the code, but then the call will cost more than the branch of if, unless it is private and can be optimized, but the use of using prevents optimization inline, then it will be much more expensive, it doesn’t pay.

There is only one detail the first example does something different from the second, so if this was not an accident none of this is worth and then the second should be followed by the simple fact that it does what must be done in both cases. If it was an accident, it’s one of the reasons to avoid making two separate codes, it seems like you’re doing the same thing and you’re not, so I value DRY a lot, the question is whether the code is canonical or not to decide what it should be, so I strongly recommend reading the other question linked.

0

Your question concerns design standards, i.e., the answer will depend on this requirement. But, Both cases work, but using the Overloading gets much more elegant, in addition to making it easy to understand.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.