ambiguity


Ambiguity of function overloading - Integers vs. Doubles


Suppose I wish to have 2 functions, one that generates a random integer within a given range, and one that generates a random double within a given range.
int GetRandomNumber( int min, int max );
double GetRandomNumber( double min, double max );
Notice that the method names are the same. I'm trying to decide whether to name the functions that or...
int GetRandomInteger( int min, int max );
double GetRandomDouble( double min, double max );
The first option has the benefit of the user not having to worry about which one they are calling. They can just call GetRandomNumber with integers or doubles and get a result.
The second option is more explicit in the names, but it reveals unneeded information to the caller.
I know this is petty, but I care about petty things.
Edit: How would C++ behave regarding implicit conversion.
Example:
GetRandomNumber( 1, 1 );
This could be implicitly converted for the GetRandomNumber function for the double version. Obviously I don't want this to occur. Will C++ use the int version before the double version?
I prefer your second example, it is explicit and leaves no ambiguity in interpretation. It is better to err on the side of being explicit in method names to clearly illuminate the purpose and function of that member.
The only downside to your approach is that you have coupled the name of the method to the return type which is not ideal in the event that you want to change the return type of one of these methods. However in that case I would be better to add a new method and not break compatibility in your API anyways.
I prefer the second version. I like overloading a function when ultimately the two functions do the same thing; in this case they return different types so they're not quite the same thing. Another possibility if your language supports it is to define it as a generic/template method, like:
T GetRandom<T>(T min, T max);
A function name should tell what the function does; I do not see a point in cramming the types into the names. So definitely go for overloading - that's what it is for.
I prefer the overloaded method. Look at the Math.Min() method in .NET. It has 11 overloads: int, double, byte, etc.
I usually prefer the first example because it doesn't pollute the namespace. For example when calling it, if I pass ints, I'm expecting to get back an int. If I pass in doubles, I probably expect to get back a double. The compiler will give you an error if you write:
//this causes an error
double d = GetRandomNumber(1,10);
so it's not really a big issue. and you can always cast the arguments if you need an int but have doubles for input...
In some of languages you can not vary the return type of overloaded functions this would require the second example.
Assuming C++, the second also avoids problems with ambiguity. If you said:
GetRandomNumber( 1, 5.0 );
which one should be used? In fact, it is a compilation error.
I think the ideal solution would be
Int32.GetRandom(int min, int max)
Double.GetRandom(double min, double max)
but, alas, static extension method are not possible (yet?).
The .net Framwork seems to favor the first option (System.Math class):
public static decimal Abs(decimal value)
public static int Abs(int value)
Like Andrew, I would personally favor the second option to avoid ambiguity, although I do think this is a matter of taste.

Related Links

Ambuigity with reserved keyword (?)
Semantics-directed parser combinators
How to iterate “along” a Marpa parse forest rather than “through” its parse trees?
Checking Ambiguious Grammar
JavaCC Ambiguities: How do I tell the parser to chose a certain match from the the list of “longer matches”?
Ambiguity of function overloading - Integers vs. Doubles

Categories

HOME
jboss
ionic2
android-espresso
generics
windows-8.1
ontouchlistener
qc
bots
softlayer
tcsh
ios-simulator
nsis
elisp
ipv6
stored-procedures
mithril.js
firebase-dynamic-links
locationmanager
mailmerge
pm2
multi-step
trace32
django-rq
asciimath
cocos2d-android
susy-sass
cloudsim
libgit2
azure-machine-learning
perfino
offline-caching
marquee
python-imageio
macromedia
sfml
dnsmasq
lxml
transform
telephonymanager
reset
plane
uipickerview
doctrine-extensions
jshint
photoswipe
lightning-workbench
pen
popen
patternlab.io
1010
kendo-treeview
oid
system.net.mail
odp.net-managed
apple-configurator
i2b2
cryptojs
extend
gql
angular-amd
com0com
abstract-factory
ipod
nsmutabledata
directorysearcher
fasterxml
global-scope
window-resize
incognito-mode
roxy-fileman
lsa
cloo
smips
facebook-game-groups
discovery
insertonsubmit
oembed
android-dialog
pageheap
firebird1.5
quartz-core
memory-pool
nsconnection
mongrel
explicit
dm
rawcontacts
external-sorting
prism-2

Resources

Encrypt Message