Skip to main content

Questions tagged [method-overloading]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
1 answer
110 views

Should I use method overloading or method overriding when creating converter service

In a Java Spring API, I'm implementing GeoJson Conversion Service to convert different types to geojson, I have GeoJsonConversionService interface, and one implementation is ...
zyydoosh's user avatar
  • 103
5 votes
1 answer
215 views

Is tag dispatch as used in CppCoreGuidelines T.65 antiquated?

The CppCoreGuidelines contain the following: T.65: Use tag dispatch to provide alternative implementations of a function [...] Example struct pod_tag {}; struct non_pod_tag {}; template<class T&...
Jan Schultke's user avatar
1 vote
1 answer
514 views

Is using lambdas and overload resolution a recommended way to write a visitor for a variant?

If I have a discriminated union and want to write a function piecewise, the following code works just fine, but is taking advantage of some fairly tricky (at least to me) stuff involving overload ...
Daniel McLaury's user avatar
0 votes
3 answers
192 views

Handling Objects with Same Output but different required inputs

I have various types of financial securities. Each one of these securities shares a common set of methods. For instance, they all pay some amount of cash interest between two dates. Each security has ...
cpage's user avatar
  • 57
0 votes
1 answer
118 views

What design pattern (if so) did I apply? How can I further improve it?

Suppose I have a program.c that needs element_123 to do some operations, and element_123 can be accessed by including agent.h /*program.c*/ #include "agent.h" uint32_t element_123 = 0; ...
Andy Lin's user avatar
  • 185
1 vote
1 answer
86 views

Providing two methods single + collection vs. only the collection

Edited Question based on the comments and great feedback (thanks) I have a REST controller exposing two GET endpoints. GetById - taking a single id (string) GetById - taking multiple id's (a ...
lucniner's user avatar
1 vote
3 answers
133 views

Good etiquette for 2 optional arguments that can't both be used

I'll demonstrate with an example of the normal distribution in Python. def norm_pdf(x, mu=0, v=1, p=1): """Returns un-normalized probability density of normal distribution at x. mu: mean v: ...
chausies's user avatar
  • 165
2 votes
3 answers
967 views

When is it appropriate to reuse a method for another method?

I am writing a program that describes different properties on a single Management Company's plot of land. For this program there are 3 overloaded addProperty method's. My question is I can reuse the ...
Alec Felix's user avatar
1 vote
1 answer
2k views

UML - Overload operations with different parameters and a different return type

I would like to know if it is allowed in the UML to specify a different return type for overloaded operations. This is possible for methods in Java: public class C { public int addOne(int a) { ...
xoric's user avatar
  • 51
-1 votes
2 answers
121 views

Should we override all method overloads

Usually method overloads delegate their parameters to the more detailed overloads with default values. here is an example A(x) => A(x, null); A(x, y) => A(x, y, null); A(x, y, z) => ...; ...
M.kazem Akhgary's user avatar
36 votes
5 answers
7k views

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<T>(int id) vs T FindById<T>(int id). Is there ...
Konrad's user avatar
  • 1,559
2 votes
0 answers
460 views

How to handle a large number of optional parameters

I am currently developing on a small library allowing to read and write Java .properties files while retaining all the formatting (comments, whitespace, etc.): https://github.com/hupfdule/apron This ...
radlan's user avatar
  • 129
3 votes
1 answer
30 views

Identical functions in two different assembllies - alternatives?

So I have a function in our application's Business Edits assembly (which references the Data Access assembly) which I've found a need to use in the Data Access assembly. I like the idea of keeping ...
j.i.h.'s user avatar
  • 141
2 votes
1 answer
1k views

REST client behavior determined by parameter type

It is generally agreed that overloading a method should not change its behavior, but how much of a method's behavior should be kept consistent? Take for example a REST API client which is responsible ...
IAmHereToParticipate's user avatar
1 vote
2 answers
239 views

Overloaded methods and optional arguments - is this a bad pattern to follow?

I'm creating an interface to record navigation history. First thing is, we have three types of records: Recordable (the default behaviour), Revisitable (a user can look at their history and revisit ...
HorusKol's user avatar
  • 4,131
63 votes
10 answers
12k views

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 ...
Snoop's user avatar
  • 2,738
4 votes
2 answers
1k views

C# Possible method name conflicts with optional parameters - why it is not forbidden? [closed]

Let's have a simple class, and main method: using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { ...
boucekv's user avatar
  • 401
5 votes
3 answers
12k views

Recommended value to pass instead of String parameter for a method in java

We have a method called attachDevice(Device device) which has only one argument. We had a situation to overload this method with one more parameter as like attachDevice(Device device, String ...
S.K. Venkat's user avatar
15 votes
5 answers
6k views

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 ...
user2638180's user avatar
1 vote
3 answers
2k views

How is this pattern called?

Is there a name for this pattern where an API offers several variations of a method (differing in number of parameters) and internally forwards them with default parameters like this: public void ...
fweigl's user avatar
  • 195
36 votes
2 answers
3k views

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, ...
Alex's user avatar
  • 470
0 votes
1 answer
218 views

Static and not-static: programmer quantum theory

Let me illustrate using the PHP language. The discussion here is, how should I do exactly to solve this problem in a clear and unambiguous mode. Imagine that I have a class called Path. This class is ...
David Rodrigues's user avatar
19 votes
12 answers
6k views

Is method overloading anything more than syntactic sugar? [closed]

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 ...
Aviv Cohn's user avatar
  • 21.4k
7 votes
3 answers
967 views

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 ...
siva.k's user avatar
  • 181
0 votes
2 answers
3k views

what's the point of method overloading? [duplicate]

I am following a textbook in which I have just come across method overloading. It briefly described method overloading as: when the same method name is used with different parameters its called method ...
David's user avatar
  • 137
3 votes
1 answer
577 views

Different scoring algorithms for different competition elements

I am creating a scoring system for a competition that is somewhat obscure, but it resembles the Olympics in terms of its high-level structure. Therefore, I will ask my question in terms of an ...
MikeWazz's user avatar
0 votes
3 answers
462 views

Overloading Methods With Different Behavior - Style

I have a style question about overloading methods/constructors. I have a constructor which does something very simple, and then calls a method with some side effects. Sometimes however I don't want ...
ckb's user avatar
  • 181
0 votes
3 answers
1k views

When should method overloads be refactored?

When should code that looks like: DoThing(string foo, string bar); DoThing(string foo, string bar, int baz, bool qux); ... DoThing(string foo, string bar, int baz, bool qux, string more, string ...
Ben Heley's user avatar
1 vote
1 answer
413 views

Compiler design decision for dynamic method invocation

I asked about Compiler interpretation of overriding vs overloading on StackOverflow, and got good answers, but this led me to another question that I'm not sure is appropriate for SO, but I think is ...
Steve P.'s user avatar
  • 151
3 votes
1 answer
3k views

Javadoc for a static "overloaded" method

My problem is the following: I have an interface that requires its implementations to implement a static method, namely void load(). However, until Java 8, it seems we won't get static methods in ...
Mog's user avatar
  • 141
6 votes
3 answers
5k views

Is function overloading in general considered Evil? [closed]

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 ...
Hossein's user avatar
  • 217
8 votes
4 answers
845 views

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 ...
Ronald Wildenberg's user avatar
41 votes
2 answers
67k views

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. ...
user1179459's user avatar
  • 1,183
12 votes
2 answers
4k views

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's user avatar
  • 409
13 votes
3 answers
618 views

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 ...
Willem D'Haeseleer's user avatar
13 votes
1 answer
3k views

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; } } $...
Jon's user avatar
  • 527
11 votes
3 answers
24k views

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 ...
blahman's user avatar
  • 386
17 votes
4 answers
5k views

Should we rename overloaded methods?

Assume an interface containing these methods : Car find(long id); List<Car> find(String model); Is it better to rename them like this? Car findById(long id); List findByModel(String model);...
Mik378's user avatar
  • 3,888
6 votes
6 answers
11k views

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'...
tskuzzy's user avatar
  • 752