General programming - calling a non void method but not using value

倾然丶 夕夏残阳落幕 提交于 2019-12-24 08:15:19

问题


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.


回答1:


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

int val = printf("Hello World");



回答2:


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.




回答3:


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



回答4:


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.




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/1231287/general-programming-calling-a-non-void-method-but-not-using-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!