函数重载的歧义 - 整数与双精度数

发布于 2024-08-15 19:56:02 字数 644 浏览 1 评论 0原文

假设我希望有 2 个函数,一个生成给定范围内的随机整数,另一个生成给定范围内的随机双精度数。

int GetRandomNumber( int min, int max );
double GetRandomNumber( double min, double max );

请注意,方法名称是相同的。我正在尝试决定是否将函数命名为...

int GetRandomInteger( int min, int max );
double GetRandomDouble( double min, double max );

第一个选项的好处是用户不必担心他们正在调用哪个函数。他们只需使用整数或双精度数调用 GetRandomNumber 即可获得结果。

第二个选项的名称更明确,但它向调用者透露了不需要的信息。

我知道这是小事,但我关心小事。

编辑:C++ 在隐式转换方面将如何表现。

示例:

GetRandomNumber( 1, 1 ); 

这可以隐式转换为双精度版本的 GetRandomNumber 函数。显然我不希望这种情况发生。 C++ 会在使用 double 版本之前使用 int 版本吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

情场扛把子 2024-08-22 19:56:02

我更喜欢你的第二个例子,它很明确,并且在解释上没有任何歧义。最好在方法名称中明确地说明该成员的目的和功能。

您的方法的唯一缺点是您将方法名称与返回类型耦合在一起,如果您想要更改这些方法之一的返回类型,这并不理想。但是在这种情况下,我最好添加一个新方法,并且无论如何都不要破坏 API 中的兼容性。

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.

染柒℉ 2024-08-22 19:56:02

我更喜欢第二个版本。当最终两个函数做同样的事情时,我喜欢重载一个函数;在这种情况下,它们返回不同的类型,因此它们不完全相同。如果您的语言支持它,另一种可能性是将其定义为通用/模板方法,例如:

T GetRandom<T>(T min, T max);

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);
漫雪独思 2024-08-22 19:56:02

函数名应该说明该函数的作用;我认为将类型塞进名称中没有意义。所以一定要超载——这就是它的用途。

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.

两个我 2024-08-22 19:56:02

我更喜欢重载方法。查看 .NET 中的 Math.Min() 方法。它有 11 个重载:int、double、byte 等。

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

情魔剑神 2024-08-22 19:56:02

我通常更喜欢第一个示例,因为它不会污染命名空间。例如,在调用它时,如果我传递整数,我期望返回一个整数。如果我通过了双打,我可能希望得到双打。如果你这样写,编译器会给你一个错误:

//this causes an error
double d = GetRandomNumber(1,10);

所以这并不是一个大问题。如果你需要一个 int 但有双精度输入,你总是可以转换参数......

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

青春有你 2024-08-22 19:56:02

在某些语言中,您无法改变重载函数的返回类型,这需要第二个示例。

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

慢慢从新开始 2024-08-22 19:56:02

假设使用 C++,第二个也避免了歧义问题。如果你说:

GetRandomNumber( 1, 5.0 );

该用哪一个?事实上,这是一个编译错误。

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.

魔法少女 2024-08-22 19:56:02

我认为理想的解决方案是,

Int32.GetRandom(int min, int max)
Double.GetRandom(double min, double max)

但是,唉,静态扩展方法是不可能的(还吗?)。

.net Framwork 似乎更倾向于第一个选项(System.Math class< /a>):

public static decimal Abs(decimal value)
public static int Abs(int value)

像安德鲁一样,我个人倾向于第二种选择以避免歧义,尽管我确实认为这是一个品味问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文