How to indicate when purposely ignoring a return value

后端 未结 1 1942
鱼传尺愫
鱼传尺愫 2021-02-13 01:00

In some situations using C/C++, I can syntactically indicate to the compiler that a return value is purposely ignored:

int SomeOperation()
{
    // Do the operat         


        
相关标签:
1条回答
  • 2021-02-13 01:49

    I can only think of one situation, when a "return value" is not allowed to be ignored in C#: when an error occurred. This should be provided by throwing an exception, which makes it impossible to be ignored.

    In other cases, it is (or better: must be) completely safe and not smelly at all to ignore return values.

    I still can't see the point. Why should this improve the code? You specify to ignore the return value by purpose by not assigning it to a variable.

    • If you don't need this value in your code, everything is fine.
    • If you need it, you won't be able to write your code.
    • If there is a special case which must be handled and must never be implicitly ignored, an exception should be thrown.
    • If the called method did not have a return value and gets one later, it must be designed to not break existing code which ignores it. The existing calling code does not change.

    Did I forget a case?

    0 讨论(0)
提交回复
热议问题