一般编程 - 调用非 void 方法但不使用值
这是一般编程,但如果它有所作为,我正在使用 Objective-C。 假设有一个方法返回一个值,并且还执行一些操作,但您不关心它返回的值,只关心它所做的事情。 您是否会像调用 void 一样调用该方法? 或者将结果放入变量中然后将其删除或忘记? 说出你的看法,如果你遇到这种情况你会怎么做。
This is general programming, but if it makes a difference, I'm using objective-c. Suppose there's a method that returns a value, and also performs some actions, but you don't care about the value it returns, only the stuff that it does. Would you just call the method as if it was void? Or place the result in a variable and then delete it or forget about it? State your opinion, what you would do if you had this situation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一个常见的例子是 printf,它返回一个 int...但你很少看到这个:
A common example of this is printf, which returns an int... but you rarely see this:
是的,只需调用该方法就好像它是无效的一样。 你可能一直在这样做,却没有注意到。 赋值运算符“=”实际上返回一个值,但很少使用。
Yeah just call the method as if it was void. You probably do it all the time without noticing it. The assignment operator '=' actually returns a value, but it's very rarely used.
这取决于环境(语言、工具、编码标准……)。
例如,在 C 中,完全有可能在不使用函数值的情况下调用函数。 对于像 printf 这样返回 int 的函数,它总是会完成。
有时不使用值会导致警告,这是不希望的。 将值分配给变量然后不使用它只会导致有关未使用变量的另一个警告。 对于这种情况,解决方案是通过在调用前添加 (void) 将结果强制转换为 void,例如
It depends on the environment (the language, the tools, the coding standard, ...).
For example in C, it is perfectly possible to call a function without using its value. With some functions like printf, which returns an int, it is done all the time.
Sometimes not using a value will cause a warning, which is undesirable. Assigning the value to a variable and then not using it will just cause another warning about an unused variable. For this case the solution is to cast the result to void by prefixing the call with (void), e.g.
实际上,这里有两个独立的问题:
第 2 个问题的答案是响亮的“否”——当然,除非您使用的语言是非法的(我想到的是早期的 Turbo Pascal)。 定义一个变量只是为了丢弃它是绝对没有意义的。
第一部分并不那么容易。 一般来说,返回值是有原因的 - 对于幂等函数,结果是函数的唯一目的; 对于非幂等,它通常表示某种返回代码,表示操作是否正常完成。 当然也有例外 - 例如方法链接。
There are two separate issues here, actually:
The answer to #2 is a resounding "NO" - unless, of course, you're working with a language where that would be illegal (early Turbo Pascal comes to mind). There's absolutely no point in defining a variable only to throw it away.
First part is not so easy. Generally, there is a reason value is returned - for idempotent functions the result is function's sole purpose; for non-idempotent it usually represents some sort of return code signifying whether operation was completed normally. There are exceptions, of course - like method chaining.
如果这在 .Net 中很常见(例如),则可能存在代码破坏 CQS< 的问题/a>.
当我调用一个返回我忽略的值的函数时,通常是因为我在测试中执行此操作以验证行为。 下面是 C# 中的一个示例:
我并不真正关心返回类型,我只是想验证是否在假对象上调用了方法。
If this is common in .Net (for example), there's probably an issue with the code breaking CQS.
When I call a function that returns a value that I ignore, it's usually because I'm doing it in a test to verify behavior. Here's an example in C#:
I don't really care about the return type, I just want to verify that a method was called on a fake object.
在 C 语言中,这很常见,但有些地方可以这样做,有些地方则不然。 更高版本的 GCC 有一个 函数属性 这样你就可以在使用函数而不检查返回值时收到警告:
上次我使用此功能时,无法关闭生成的警告,这会在您编译不想修改的第 3 方代码时出现问题。 此外,当然没有办法检查用户是否确实对返回值做了一些明智的事情。
In C it is very common, but there are places where it is ok to do so and other places where it really isn't. Later versions of GCC have a function attribute so that you can get a warning when a function is used without checking the return value:
Last time I used this there was no way of turning off the generated warning, which causes problems when you're compiling 3rd-party code you don't want to modify. Also, there is of course no way to check if the user actually does something sensible with the returned value.