What’s the best practice when returning something from a routine? Should I return a status bit always or just on failure? For example:
从常规中返回东西时最好的做法是什么?我应该总是或仅仅在失败时返回状态位吗?例如:
Return(0, “Failed because….”) on failure,
Return(1, success_value, second_success_value) on success.
成功时返回(0,“因......失败”),返回(1,success_value,second_success_value)。
Or
Return(0, “Failed because….”) on failure,
Return( success_value, second_success_value) on success.
失败时返回(0,“因......失败”),成功时返回(success_value,second_success_value)。
I usually program in Perl, but I guess the question stands for whatever language I might try to program in. Thanks!
我通常在Perl中编程,但我想这个问题代表我可能尝试编程的任何语言。谢谢!
3 个解决方案
#1
The answer is very language dependent. You should follow the idiom for the language you are using. The idiom for Perl, which you mentioned, is to return zero(0) for success and other values for failure. If you need to return multiple values, as in your example, one of them could always be the success/failure code.
答案非常依赖于语言。你应该按照你正在使用的语言的习语。你提到的Perl的习惯用法是返回零(0)表示成功,其他值表示失败。如果需要返回多个值(如示例所示),其中一个值可能始终为成功/失败代码。
If you're using a language that supports exceptions (eg. Java, C#, C++) then you should indicate exceptional conditions (eg. failures) using an exception. If the routine completes normally (ie. no exceptions) then it can be assumed to have succeeded and any returned values can be used safely.
如果您使用支持异常的语言(例如Java,C#,C ++),那么您应该使用异常指示异常条件(例如,失败)。如果例程正常完成(即没有例外),则可以假定它已成功并且可以安全地使用任何返回的值。
#2
In languages that support them, you should throw descriptive exceptions.
在支持它们的语言中,您应该抛出描述性异常。
Where applicable, you should throw exceptions of the correct type (eg, ArgumentNullException
or InvalidOperationException
in .Net)
在适用的情况下,您应该抛出正确类型的异常(例如,.Net中的ArgumentNullException或InvalidOperationException)
#3
hey Akers, I've asked myself a similar question in the past...
嘿,阿克斯,我过去曾问自己一个类似的问题......
Some of these answers might help you
其中一些答案可能会对您有所帮助
Is it bad practice to return Exceptions from your methods
从您的方法返回异常是不好的做法
The basic conclusion I came to is to only return something, anything, if the caller requires it.
我得出的基本结论是只返回一些东西,如果调用者需要的话。
So, for the most part, I found its better to just make my method Void, and have it throw a meaningful exception if anything goes wrong. If everything goes ok, then I, as the caller, don't really care
因此,在大多数情况下,我发现将我的方法设置为Void更好,并且如果出现任何问题,它会抛出一个有意义的异常。如果一切顺利,那么作为来电者,我并不在乎
#1
The answer is very language dependent. You should follow the idiom for the language you are using. The idiom for Perl, which you mentioned, is to return zero(0) for success and other values for failure. If you need to return multiple values, as in your example, one of them could always be the success/failure code.
答案非常依赖于语言。你应该按照你正在使用的语言的习语。你提到的Perl的习惯用法是返回零(0)表示成功,其他值表示失败。如果需要返回多个值(如示例所示),其中一个值可能始终为成功/失败代码。
If you're using a language that supports exceptions (eg. Java, C#, C++) then you should indicate exceptional conditions (eg. failures) using an exception. If the routine completes normally (ie. no exceptions) then it can be assumed to have succeeded and any returned values can be used safely.
如果您使用支持异常的语言(例如Java,C#,C ++),那么您应该使用异常指示异常条件(例如,失败)。如果例程正常完成(即没有例外),则可以假定它已成功并且可以安全地使用任何返回的值。
#2
In languages that support them, you should throw descriptive exceptions.
在支持它们的语言中,您应该抛出描述性异常。
Where applicable, you should throw exceptions of the correct type (eg, ArgumentNullException
or InvalidOperationException
in .Net)
在适用的情况下,您应该抛出正确类型的异常(例如,.Net中的ArgumentNullException或InvalidOperationException)
#3
hey Akers, I've asked myself a similar question in the past...
嘿,阿克斯,我过去曾问自己一个类似的问题......
Some of these answers might help you
其中一些答案可能会对您有所帮助
Is it bad practice to return Exceptions from your methods
从您的方法返回异常是不好的做法
The basic conclusion I came to is to only return something, anything, if the caller requires it.
我得出的基本结论是只返回一些东西,如果调用者需要的话。
So, for the most part, I found its better to just make my method Void, and have it throw a meaningful exception if anything goes wrong. If everything goes ok, then I, as the caller, don't really care
因此,在大多数情况下,我发现将我的方法设置为Void更好,并且如果出现任何问题,它会抛出一个有意义的异常。如果一切顺利,那么作为来电者,我并不在乎