Jun 27

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

 


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.

 


2) Avoid changing name of parameters for Overloaded Methods

Bad Example:

 

 

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:

 


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

Bad Example:

 

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

Good Example:

 

Permanent link to this article: http://zappmania.in/2012/06/27/method-overloading-best-practise-dos-and-dont.htm

Leave a Reply

Your email address will not be published.