tags:

views:

157

answers:

8

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?

+3  A: 

I prefer your second example, it is explicit and leaves no ambiguity in interpretation. It is better to air 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.

Andrew Hare
.NET is also full of examples using the first method. Like in Console.WriteLine() and everything in System.Math... ;)
dan
A: 

In some of languages you can not vary the return type of overloaded functions this would require the second example.

Martin Brown
+2  A: 

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);
ChrisW
Ew, how do you restrict to numeric types?
Khanzor
+2  A: 

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.

hstoerr
A: 

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.

anon
+1  A: 

I prefer the overloaded method. Look at the Math.Min() method in .NET. It has 11 overloads: int, double, byte, etc.

Sam
+1  A: 

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...

dan
What language are you referring to, this is perfectly fine in C and C++.
Andreas Brinck
oops, i was referring to c#. i think java would give an error for that too.
dan
A: 

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.

Heinzi