Reputation: 2982
When faced with a class that has multiple similar methods operating on different parameter types, do you include some kind of description of the parameters in the method name, or do you keep the name the same and trust that the parameters themselves provide enough information? Contrast the two examples given below:
interface Option1 {
update(ObjectA);
update(ObjectB);
update(List<Object>);
}
interface Option2 {
updateA(ObjectA);
updateB(ObjectB);
updateAll(List<Object>);
}
I've heard the following arguments:
Upvotes: 0
Views: 556
Reputation: 86381
It depends. Method overloading exists because it is useful. However, it can also cause you grief.
If you are considering overloading, consider:
For example:
String.indexOf()
is overloaded. All the overload have the same intent. There will be no additional indexOf() methods mixed into the leaf class. set()
-- all of the above questions may be answered "yes." Upvotes: 1
Reputation: 117220
For a statically typed language that supports method overloading, option 1.
For dealing with object hierarchies, option 1.
For all other cases, I would suggest option 2.
My 2 cents. :)
Upvotes: 0