Sunday, December 22, 2013

METHOD OVERLOADING BEST PRACTICE – DO’S AND DON’T

METHOD OVERLOADING BEST PRACTICE – DO’S AND DON’T


Let’s briefly 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)

 



METHOD OVERLOADING BEST PRACTICE – DO’S AND DON’T

No comments:

Post a Comment