检查参考参数值或返回bool?
我正在使用具有以下签名的方法:
public static bool TryAuthenticate(string userName, string password,
string domainName, out AuthenticationFailure authenticationFailure)
该方法声明:boolauthentiated = false;
然后继续对用户进行身份验证。
每当 authenticated
设置为 true 或 false 时,authenticationFailure
就会相应地设置为 AuthenticationFailure.Failure
或 AuthenticationFailure.Success
。
所以基本上我可以使用authenticationFailure或方法的返回值来检查结果。然而,在同一个方法中使用这两种方法似乎毫无意义地违反了 DRY。
只是为了澄清,authenticationFailure 没有在该方法的其他任何地方使用,因此它看起来完全是多余的。
目前我正在这样做:
public static bool IsValidLDAPUser(string username, string password, string domain)
{
var authenticationStatus = new AuthenticationFailure();
if (ActiveDirectoryAuthenticationService.TryAuthenticate(username, password, domain, out authenticationStatus))
return true;
else return false;
}
但我可以这样做并得到类似结果:
public static AuthenticationFailure IsValidLDAPUser(string username, string password, string domain)
{
var authenticationStatus = new AuthenticationFailure();
ActiveDirectoryAuthenticationService.TryAuthenticate(username, password, domain, out authenticationStatus)
return authenticationStatus;
}
- 为什么你会有一个引用参数与返回值做同样的事情?
- 我应该使用哪一个来检查结果?它有什么不同吗?
- 这只是一个糟糕的代码的情况还是我错过了重点?
提前致谢!
I am using a method that has the following signature:
public static bool TryAuthenticate(string userName, string password,
string domainName, out AuthenticationFailure authenticationFailure)
The method declares: bool authenticated = false;
then goes on to authenticate the user.
Whenever authenticated
is set to true or false, authenticationFailure
is set to AuthenticationFailure.Failure
or AuthenticationFailure.Success
correspondingly.
So basically I can use either authenticationFailure or the return value of the method to check the result. However it seems to be a pointless violation of DRY to have these two approaches in the same method.
Just to clarify, authenticationFailure is not used anywhere else in the method so it appears to be totally redundant.
At the moment I'm doing this:
public static bool IsValidLDAPUser(string username, string password, string domain)
{
var authenticationStatus = new AuthenticationFailure();
if (ActiveDirectoryAuthenticationService.TryAuthenticate(username, password, domain, out authenticationStatus))
return true;
else return false;
}
But I could do this and get a similar result:
public static AuthenticationFailure IsValidLDAPUser(string username, string password, string domain)
{
var authenticationStatus = new AuthenticationFailure();
ActiveDirectoryAuthenticationService.TryAuthenticate(username, password, domain, out authenticationStatus)
return authenticationStatus;
}
- Why would you have a reference parameter that does the same thing as the return value?
- Which one should I use to check the result and does it make any difference?
- Is this just a case of bad code or am I missing the point?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通常,错误代码不仅仅是成功或失败。也许此方法的设计者将为所有不同的故障类型添加更多枚举。
有时,也有不止一种成功类型——例如,HTTP 在 200 和 300 块中有很多返回代码,这些代码在某种程度上都被视为成功。因此,布尔值通常会告诉您是否成功,而枚举则提供更准确的信息。
有很多方法可以做到这一点,这一种很不寻常,但如果他们计划添加更多代码,并不反对 DRY。
另一种方法是封装到具有枚举和 IsSuccess 属性的
Result
类中。您甚至可以提供到 bool 的转换,以使其易于在 if 语句中使用。Often there are more error codes than just success or failure. Perhaps the designer of this method is going to add more enumerations for all the different failure types.
Sometimes, there is also more than one success type -- e.g. HTTP has a lot of return codes in the 200 and 300 block that would all be considered success in some way. So the bool tells you generally if it was successful or not, and the enum gives more exact information.
There are a lot of ways to do it, and this one is unusual, but not against DRY if they plan to add more codes.
Another way is to just encapsulate into a
Result
class that has the enum and aIsSuccess
property. You could even provide a conversion to bool to make it easy to use in if statements.首先,你的枚举变量名称对我来说似乎有点扭曲。
AuthenticationFailure.Success
没有多大意义!它应该是 AuthenticationResult 或其他东西。另外,由于在您的方法中您只关心身份验证是否成功,因此您应该返回 bool。您在想到 DRY 的同时也想到了 KISS!您仍然可以使用枚举来表示您可能添加的其他错误代码,但是一看方法的返回值就应该知道它是否成功。如果您希望查找失败原因的详细信息,请使用作为“out”参数传递的枚举。
First off, your enum variable name seems a little twisted to me.
AuthenticationFailure.Success
does not make much sense! It should beAuthenticationResult
or something. Also, since in your method you just care whether authentication was successful, you should return bool. You are thinking of DRY but also think of KISS!You can still make use of the enum for other error codes that you might add, but one look at the return value of your method should tell you whether it was successful or not. If you wish to find the details of why it was not successful, then use the enum that was passed as the 'out' parameter.
如果函数仅返回一个值,则应优先使用常规函数返回值而不是
out
或ref
参数。我想说这对于名为 IsXyz 的方法更为重要(重点是 is),并且以这种方式命名的方法应该返回
bool
。If a function returns only one value then the regular function return value should be used in preference to an
out
orref
parameter.I would say this is even more important for methods named IsXyz (the emphasis being on is) and methods named in this way should return
bool
.与返回值相比,Ref 参数不太容易使用和理解。但是,在某些情况下使用 ref 参数可能会具有优势。对于这种情况,如果我们假设分配 AuthenticationFailure 很耗时,那么使用 ref 参数是合适的,因为在身份验证成功的情况下不需要分配。
无论如何,在这种情况下,我更喜欢使用 AuthenticationResult 返回类型,因为应该不会影响性能。
Ref parameters are not so easy to use and understand comparing to return value. However, there are some situation that using ref parameters can be advantage. For this case, if we assume allocating AuthenticationFailure is time-consuming, so using ref parameter is suitable because no allocation will be needed in case of authentication success.
Anyway, In this case I prefer using AuthenticationResult return type, as there should be no performance impact.
恕我直言,我不认为这是 bool 与 ref 的问题。对我来说,参考中返回的内容似乎是错误的。
我希望看到以下方法
,
一种是当您不关心原因时使用的方法,一种是您关心原因时使用的方法。例如,
这允许调用代码进行身份验证,然后在没有捕获的情况下执行某些操作
,或者如果他们需要额外的信息,则使用捕获,
例如
现在,如果没有用户或令牌的概念,那么可能不需要尝试模式
IMHO I don't think this is bool vs ref thing. To me it looks like the the wrong thing is being returned in the ref.
I would expect to see the following methods
and
One for when you don't care about why and one that you do. e.g.
This allows the calling code to do authenticate and then do something without a catch
Or if they need that extra info then use a catch
e.g.
Now if there's no concept of a user or token then the Try Pattern probably isn't needed