一般编程 - 调用非 void 方法但不使用值

发布于 2024-07-30 08:20:30 字数 156 浏览 8 评论 0原文

这是一般编程,但如果它有所作为,我正在使用 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 技术交流群。

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

发布评论

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

评论(6

苍白女子 2024-08-06 08:20:30

一个常见的例子是 printf,它返回一个 int...但你很少看到这个:

int val = printf("Hello World");

A common example of this is printf, which returns an int... but you rarely see this:

int val = printf("Hello World");
情愿 2024-08-06 08:20:30

是的,只需调用该方法就好像它是无效的一样。 你可能一直在这样做,却没有注意到。 赋值运算符“=”实际上返回一个值,但很少使用。

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.

巷雨优美回忆 2024-08-06 08:20:30

这取决于环境(语言、工具、编码标准……)。

例如,在 C 中,完全有可能在不使用函数值的情况下调用函数。 对于像 printf 这样返回 int 的函数,它总是会完成。

有时不使用值会导致警告,这是不希望的。 将值分配给变量然后不使用它只会导致有关未使用变量的另一个警告。 对于这种情况,解决方案是通过在调用前添加 (void) 将结果强制转换为 void,例如

(void) my_function_returning_a_value_i_want_to_ignore().

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.

(void) my_function_returning_a_value_i_want_to_ignore().
云之铃。 2024-08-06 08:20:30

实际上,这里有两个独立的问题:

  1. 您应该关心返回值吗?
  2. 您应该将其分配给您不打算使用的变量吗?

第 2 个问题的答案是响亮的“否”——当然,除非您使用的语言是非法的(我想到的是早期的 Turbo Pascal)。 定义一个变量只是为了丢弃它是绝对没有意义的。

第一部分并不那么容易。 一般来说,返回值是有原因的 - 对于幂等函数,结果是函数的唯一目的; 对于非幂等,它通常表示某种返回代码,表示操作是否正常完成。 当然也有例外 - 例如方法链接

There are two separate issues here, actually:

  1. Should you care about returned value?
  2. Should you assign it to a variable you're not going to use?

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.

零度° 2024-08-06 08:20:30

如果这在 .Net 中很常见(例如),则可能存在代码破坏 CQS< 的问题/a>.

当我调用一个返回我忽略的值的函数时,通常是因为我在测试中执行此操作以验证行为。 下面是 C# 中的一个示例:

    [Fact]
    public void StatService_should_call_StatValueRepository_for_GetPercentageValues()
    {
        var statValueRepository = new Mock<IStatValueRepository>();

        new StatService(null, statValueRepository.Object).GetValuesOf<PercentageStatValue>();

        statValueRepository.Verify(x => x.GetStatValues());
    }

我并不真正关心返回类型,我只是想验证是否在假对象上调用了方法。

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#:

    [Fact]
    public void StatService_should_call_StatValueRepository_for_GetPercentageValues()
    {
        var statValueRepository = new Mock<IStatValueRepository>();

        new StatService(null, statValueRepository.Object).GetValuesOf<PercentageStatValue>();

        statValueRepository.Verify(x => x.GetStatValues());
    }

I don't really care about the return type, I just want to verify that a method was called on a fake object.

各自安好 2024-08-06 08:20:30

在 C 语言中,这很常见,但有些地方可以这样做,有些地方则不然。 更高版本的 GCC 有一个 函数属性 这样你就可以在使用函数而不检查返回值时收到警告:

如果具有此属性的函数的调用者不使用其返回值,则 warn_unused_result 属性会导致发出警告。 这对于不检查结果要么是安全问题要么总是错误的函数非常有用,例如 realloc。

 int fn () __attribute__ ((warn_unused_result)); 
       int foo() 
       { 
         if (fn() < 0) 返回 -1; 
         fn(); 
         返回0; 
       } 
  

导致第 5 行出现警告。

上次我使用此功能时,无法关闭生成的警告,这会在您编译不想修改的第 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:

The warn_unused_result attribute causes a warning to be emitted if a caller of the function with this attribute does not use its return value. This is useful for functions where not checking the result is either a security problem or always a bug, such as realloc.

     int fn () __attribute__ ((warn_unused_result));
     int foo ()
     {
       if (fn () < 0) return -1;
       fn ();
       return 0;
     }

results in warning on line 5.

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.

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