Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method overloading - good or bad design? [closed]

I like to overload methods to support more and more default cases. What is the performance impact of method overloading? From your experience, is it advisable to overload methods? What is the limit? What are the workarounds?

like image 570
Hari Menon Avatar asked Aug 27 '10 06:08

Hari Menon


People also ask

Is method overloading good?

If a class has multiple methods having same name but different in parameters, it is known as Method Overloading. If we have to perform only one operation, having same name of the methods increases the readability of the program.

What is the disadvantage of method overloading?

In function overloading, the main disadvantage is that the functions with different return types can't be overloaded. In the case of a static function, the same parameters cannot be overloaded.

What are the conditions for overloading?

Overloading occurs when two or more methods in one class have the same method name but different parameters. Must have at least two methods by the same name in the class. Must have a different number of parameters. If the number of parameters is the same, then it must have different types of parameters.

How method overloading can be prevented?

The final way of preventing overriding is by using the final keyword in your method. The final keyword puts a stop to being an inheritance. Hence, if a method is made final it will be considered final implementation and no other class can override the behavior.


2 Answers

Away from performance, overloading if used intensively will for sure lead to confusion once somebody starts to maintain the code, I prefer doing this:

Person GetPersonByID( int id );
Person GetPersonByName( String name );

on this:

Person GetPerson( int id );
Person GetPerson( String name );
like image 55
muaz Avatar answered Oct 13 '22 17:10

muaz


Overloading has no impact on performance; it's resolved by the compiler at compile-time.

As for design guidance, see the design guidelines:

http://msdn.microsoft.com/en-us/library/ms229029.aspx

like image 44
Brian Avatar answered Oct 13 '22 18:10

Brian