关于功能结果处理的一般问题。 [英] General Question On Handling of Function results.

查看:60
本文介绍了关于功能结果处理的一般问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常处于我想对函数的结果采取行动的情况,

但是一个简单的布尔值是不够的。例如,我可能有一个函数

叫做


isAuthorised(用户,动作)为?????


好​​吧,这个函数可能会返回一个布尔值,如果这是真的,那么确实不需要消息

,但是如果它失败则需要一些支持消息

将返回到调用代码。我看到它有几个选项。


1.)抛出一条带有消息的异常(但这往往会减慢

执行一点点??)


2.)返回一个枚举类型,但这有点麻烦,因为它

意味着我必须定义所有可能的返回组合一个数字,

可能会变得凌乱。


3.)添加一个名为message的第三个参数,可以设置为任何错误
$ b返回$ b消息。


4.)返回一个结构,其中包含一个布尔值和一个描述

结果的字符串。

在这种情况下,我可以问一下你们通常做什么,最好的是什么?b $ b练习,利弊等等。

谢谢


N先生

I am often in the situation where I want to act on the result of a function,
but a simple boolean is not enough. For example, I may have a function
called

isAuthorised ( User, Action ) as ?????

OK, this function may return a boolean, and if this is true, then no message
back is really required, but if it fails then some supporting message needs
to be returned to the calling code. As I see it there are a few options.

1.) Throw an exception with the message in ( But this tends to slow down
execution a little ?? )

2.) Return an enumeration type, but this is a little cumbersome because it
means I have to define all my possible return combinations against a number,
which could become messy.

3.) Add a third parameter called message which can be set to any error
message returned.

4.) Return a structure which contains a boolean and a string describing the
result.
Can I ask what you guys normally do in this situation, what is best
practice, pros and cons etc.
Thanks

Mr N

推荐答案

我通常使用选项4,我创建一个包含所需信息的结构,并且

返回此项我需要返回多个值


使用异常手如果你没有看到其他的方式,它会更快

如果你可以评估一个值并返回它以采取适当的行动

使用try catch语句或抛出错误


问候


Michel Posseth [MCP]


Mr Newbie <他** @ now.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP14.phx.gbl ...
I normally use option 4 , i create a structure with the required info and
return this if i need to return multiple values

use exception handling if you do not see anny other way, it is much faster
if you can evaluate a value and return this to take apropriate action as to
use a try catch statement or throwing errorr

regards

Michel Posseth [MCP]

"Mr Newbie" <he**@now.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
我是通常在我想要对
函数的结果起作用的情况下,但是简单的布尔值是不够的。例如,我可能有一个名为

isAuthorised(User,Action)的函数作为?????

好的,这个函数可能会返回一个布尔值,如果这是真的,那么确实不需要回复消息,但如果失败则需要将一些支持的消息返回给调用代码。我看到它有几个选项。

1.)抛出一个异常的消息(但这往往会减慢
执行一点??)< 2.)返回枚举类型,但这有点麻烦,因为它意味着我必须根据
数字定义所有可能的返回组合,这可能会变得混乱。

3.)添加一个名为message的第三个参数,可以设置为返回的任何错误
消息。

4.)返回一个包含布尔值的结构一个描述结果的字符串。

我可以问一下你们在这种情况下通常做什么,最好的是什么,实践,利弊等。

谢谢

N先生
I am often in the situation where I want to act on the result of a
function, but a simple boolean is not enough. For example, I may have a
function called

isAuthorised ( User, Action ) as ?????

OK, this function may return a boolean, and if this is true, then no
message back is really required, but if it fails then some supporting
message needs to be returned to the calling code. As I see it there are a
few options.

1.) Throw an exception with the message in ( But this tends to slow down
execution a little ?? )

2.) Return an enumeration type, but this is a little cumbersome because it
means I have to define all my possible return combinations against a
number, which could become messy.

3.) Add a third parameter called message which can be set to any error
message returned.

4.) Return a structure which contains a boolean and a string describing
the result.
Can I ask what you guys normally do in this situation, what is best
practice, pros and cons etc.
Thanks

Mr N



感谢您的回复。选项4当然是纯粹的方式来实现这一点和

对于我们对函数的看法非常谨慎,把事情放在参数中

返回一些东西。出于好奇,您是否反对

选项3,如果是这样的话会是什么?


-

最好的问候


无与伦比的Mr Newbie o?o

" m.posseth" < MI ***** @ nohausystems.nl>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
Thanks for your reply. Option 4 is of course the purist way to do this and
sits nicley with what we think about functions, put things in the arguments
return something back. Just out of curiosity, do you have an objection to
option 3, and if so what would it be ?

--
Best Regards

The Inimitable Mr Newbie o?o
"m.posseth" <mi*****@nohausystems.nl> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
我通常使用选项4,我创建一个包含所需信息的结构,如果我需要返回多个值,则返回此

如果你没有看到其他方式,则使用异常处理,它很多<如果你可以评估一个值并返回它以采取适当的措施来使用try catch语句或抛出错误

那么Michel Posseth [MCP]

新手先生 <他** @ now.com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP14.phx.gbl ...
I normally use option 4 , i create a structure with the required info and
return this if i need to return multiple values

use exception handling if you do not see anny other way, it is much
faster if you can evaluate a value and return this to take apropriate
action as to use a try catch statement or throwing errorr

regards

Michel Posseth [MCP]

"Mr Newbie" <he**@now.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
我经常处于这种情况我想在
函数的结果上行动,但是一个简单的布尔是不够的。例如,我可能有一个名为

isAuthorised(User,Action)的函数作为?????

好的,这个函数可能会返回一个布尔值,如果这是真的,那么确实不需要回复消息,但如果失败则需要将一些支持的消息返回给调用代码。我看到它有几个选项。

1.)抛出一个异常的消息(但这往往会减慢
执行一点??)< 2.)返回枚举类型,但这有点麻烦,因为
这意味着我必须根据
数字定义所有可能的返回组合,这可能会变得混乱。

3.)添加一个名为message的第三个参数,可以设置为返回的任何错误
消息。

4.)返回一个包含布尔值的结构一个描述结果的字符串。

我可以问一下你们在这种情况下通常做什么,最好的是什么,实践,利弊等。

谢谢

N先生
I am often in the situation where I want to act on the result of a
function, but a simple boolean is not enough. For example, I may have a
function called

isAuthorised ( User, Action ) as ?????

OK, this function may return a boolean, and if this is true, then no
message back is really required, but if it fails then some supporting
message needs to be returned to the calling code. As I see it there are a
few options.

1.) Throw an exception with the message in ( But this tends to slow down
execution a little ?? )

2.) Return an enumeration type, but this is a little cumbersome because
it means I have to define all my possible return combinations against a
number, which could become messy.

3.) Add a third parameter called message which can be set to any error
message returned.

4.) Return a structure which contains a boolean and a string describing
the result.
Can I ask what you guys normally do in this situation, what is best
practice, pros and cons etc.
Thanks

Mr N




Mr Newbie写道:
Mr Newbie wrote:
我可以问一下你们在这种情况下通常做什么,最好的是什么,实践,利弊等。
Can I ask what you guys normally do in this situation, what is best
practice, pros and cons etc.




我亲自使用过博以下解决

问题的方法之一:


1.传入ByRef字符串,通过该字符串可以返回任何消息

函数。


2.获取函数返回一个String,并使用

中的某些字符串来表示是否功能成功与否。因此,如果返回一个空的

字符串,则授权检查成功;如果消息是

返回,则检查失败。


无论哪种方式,都值得提供良好的评论水平(两者都在

函数本身和调用它的代码)来描述函数的非显而易见的

行为。


-


(O)enone



I''ve personally used both of the following approaches to solving the
problem:

1. Pass in a ByRef String, through which any messages can be returned from
the function.

2. Get the function to return a String, and use the presence of something in
the string to indicate whether the function succeeded or not. So if an empty
string is returned, the authorised check succeeded; if a message was
returned then the check failed.

Either way, it''s well worth providing a good level of commenting (both in
the function itself and the code that calls it) to describe the non-obvious
behaviour of your function.

--

(O)enone


这篇关于关于功能结果处理的一般问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆