Questions tagged [method-overloading]
39 questions
63
votes
10 answers
Is it bad practice to use a C++ compiler just for function overloading?
So I am working on a software design using C for a certain processor. The tool-kit includes the ability to compile C as well as C++. For what I am doing, there is no dynamic memory allocation available in this environment and the program is overall…
Snoop
- 2,758
- 6
- 29
- 54
41
votes
2 answers
Why PHP doesn't support function overloading?
I am wondering if one of the key features of a programming language is to have the ability to overload functions via arguments. I think it is essential in-context of the Object oriented programming.
Is it intentionally left behind and not allowed?…
user1179459
- 1,183
36
votes
5 answers
Is it enough for methods to be distinguished just by argument name (not type)?
Is it enough for methods to be distinguished just by argument name (not type) or is it better to name it more explicitly?
For example T Find(int id) vs T FindById(int id).
Is there any good reason to name it more explicitly (i.e. adding ById)…
Konrad
- 1,569
36
votes
2 answers
Why do methods that take an unlimited amount of parameters often define overloads with fewer parameters?
For instance, the System.IO.Path.Combine method in .NET has the following overloads:
Combine(params String[])
Combine(String, String)
Combine(String, String, String)
Combine(String, String, String, String)
What is the point of the last three?
The…
Alex
- 470
- 4
- 7
19
votes
12 answers
Is method overloading anything more than syntactic sugar?
Is method overloading a type of polymorphism? To me it seems like simply the differentiation of methods with the same name and different parameters. So stuff(Thing t) and stuff(Thing t, int n) are entirely different methods as far as the compiler…
Aviv Cohn
- 21,538
18
votes
4 answers
Should we rename overloaded methods?
Assume an interface containing these methods :
Car find(long id);
List find(String model);
Is it better to rename them like this?
Car findById(long id);
List findByModel(String model);
Indeed, any developer who use this API won't need to…
Mik378
- 3,926
15
votes
5 answers
Why isn't the overloading with return types allowed? (at least in usually used languages)
I don't know about all programming languages, but it's clear that usually the possibility of overloading a method taking into consideration its return type (assuming its arguments are the same number and type) is not supported.
I mean something like…
user2638180
- 383
13
votes
2 answers
Overload or Optional Parameters
When I have a function that might, or might not receive a certain parameter, is it better to overload the function, or to add optional args?
If each one has it's ups and downs - when would I use each?
JNF
- 419
- 1
- 6
- 17
13
votes
3 answers
Does one method overload an other, or are both methods "overloaded"
If I create this method
public void foo()
And then I create an overloaded version like this
public void foo( string bar )
Do we say that the second functions overloads the first, or are both methods equally "overloaded"?
This would imply (I…
13
votes
1 answer
Why is there no facility to overload static properties in PHP?
Intro
PHP allows you to overload method calls and property accesses by declaring magic methods in classes. This enables code such as:
class Foo {
public function __get($name) { return 42; }
}
$foo = new Foo;
echo $foo->missingProperty; //…
Jon
- 537
11
votes
3 answers
When is method overloading appropriate?
Suppose I am working on an existing, reasonably large system. I have an object, myObject of class MyClass (for the example's sake, suppose I'm working in Java). myObject is a composition containing a Collection, say, a List and other objects which…
blahman
- 386
8
votes
4 answers
How to prevent duplicate data access methods that retrieve similar data?
In almost every project I work on with a team, the same problem seems to creep in. Someone writes UI code that needs data and writes a data access method:
AssetDto GetAssetById(int assetId)
A week later someone else is working on another part of…
7
votes
3 answers
Overloading methods that do logically different things, does this break any major principles?
This is something that's been bugging me for a bit now. In some cases you see code that is a series of overloads, but when you look at the actual implementation you realize they do logically different things. However writing them as overloads allows…
siva.k
- 181
- 4
6
votes
3 answers
Is function overloading in general considered Evil?
Recently I found about two new programming languages(Vala and google's GO) which don't support method or function overloading and intend on not supporting them in the future ever! The creators of these languages say that overloading is evil and…
Hossein
- 217
6
votes
6 answers
How to resolve methods with the same name and parameter types?
In many cases, I want to write methods that have the same functionality for different types of inputs. This is easily accomplished by method overloading if the parameter types are different.
But what's the best (most robust) way to go about…
tskuzzy
- 752