Sunday, September 11, 2011

Method Overloading Best Practise – Do’s and Don’t

In this article i will explain what to do and what to avoid while going for method overloading.


Let's breifly understand what is Method Overloading, it is method with same name but with different arguments is called method overloading.

Example of Method Overloading
//SendEmail without Header and Footer
bool SendEmail(string Subject, string Body, string ToEmail) {}


//SendEmail with only Footer
bool SendEmail(string Subject, string Body, string ToEmail, string Footer) {}


//SendEmail with both Header and Footer
bool SendEmail(string Subject, string Body, string ToEmail, string Footer, string Header) {}

Must follow rules while doing method overloading

1 Method Body should not be repeated in every method,   don’t write same code in every method, instead, call the method by passing default parameter.


//SendEmail without Header and Footer
bool SendEmail(string Subject, string Body, string ToEmail) {
   //Since we don’t have footer, call method by passing empty string
   return SendEmail(Subject, Body, ToEmail, string.Empty);
}

//SendEmail with only Footer
bool SendEmail(string Subject, string Body, string ToEmail, string Footer) {
   //Since we don’t have header, call method by passing empty string
   return SendEmail(Subject, Body, ToEmail, Footer, string.Empty);
}

//SendEmail with both Header and Footer
bool SendEmail(string Subject, string Body, string ToEmail, string Footer, string Header) {           
    bool IsEmailSendSuccessfully = false;
   string WebsiteName = ConfigurationManager.AppSettings["WebsiteName"].ToString();
   string EmailServer = ConfigurationManager.AppSettings["EmailServer"].ToString();
   string FromEmail = ConfigurationManager.AppSettings["EmailDoNotReply"].ToString();
   string FromEmailPassword = ConfigurationManager.AppSettings["EmailPassword"].ToString();
   Body = Header + Body + Footer;
   try
  {
       MailMessage MyMailMessage = new MailMessage(FromEmail, ToEmail, Subject, Body);
       MyMailMessage.IsBodyHtml = true;
       MailAddress objMailAddr = new MailAddress(FromEmail, WebsiteName);
       MyMailMessage.From = objMailAddr;
       SmtpClient mailClient = new SmtpClient(EmailServer);
       mailClient.Credentials = new NetworkCredential(FromEmail, FromEmailPassword);
       mailClient.Send(MyMailMessage);
       IsEmailSendSuccessfully = true;
    }
    catch (Exception ex)
   {
     IsEmailSendSuccessfully = false;
     ErrorMessages.ErrorReporting(ex);
    }
    return IsEmailSendSuccessfully;
}


2 Avoid changing name of parameters for Overloaded Methods 

Bad Example:

bool SendEmail(string Subject, string Body, string ToEmail) {}


bool SendEmail(string Title, string Message, string ToEmail, string Footer) {}


bool SendEmail(string SubjectTitle, string BodyMessage, string ToEmail, string Footer, string Header) {}



In above method  you might have notice that we are using different parameter name for Subject and Body, across different method overload function, try to avoid that.

Good Example:

bool SendEmail(string Subject, string Body, string ToEmail) {}


bool SendEmail(string Subject, string Body, string ToEmail, string Footer) {}


bool SendEmail(string Subject, string Body, string ToEmail, string Footer, string Header) {}




3 Avoid changing sequence of parameters for Overloaded Methods, Ordering of overloaded method should be consistent.

Bad Example:

bool SendEmail(string Subject, string Body, string ToEmail) {}


bool SendEmail(string Body, string Subject, string Footer, string ToEmail) {}


bool SendEmail(string Footer, string Header, string Subject, string Body, string ToEmail) {}

In above examples sequence of overloaded method are not consistent.  Try to avoid that to avoid confusion and making things complex.

Good Example:
bool SendEmail(string Subject, string Body, string ToEmail) {}


bool SendEmail(string Subject, string Body, string ToEmail, string Footer) {}


bool SendEmail(string Subject, string Body, string ToEmail, string Footer, string Header) {}

No comments:

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape