检查参考参数值或返回bool?

发布于 2024-09-16 12:49:35 字数 1449 浏览 8 评论 0原文

我正在使用具有以下签名的方法:

public static bool TryAuthenticate(string userName, string password, 
    string domainName, out AuthenticationFailure authenticationFailure)

该方法声明:boolauthentiated = false;然后继续对用户进行身份验证。

每当 authenticated 设置为 true 或 false 时,authenticationFailure 就会相应地设置为 AuthenticationFailure.FailureAuthenticationFailure.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

屋檐 2024-09-23 12:49:35

通常,错误代码不仅仅是成功或失败。也许此方法的设计者将为所有不同的故障类型添加更多枚举。

有时,也有不止一种成功类型——例如,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 a IsSuccess property. You could even provide a conversion to bool to make it easy to use in if statements.

征棹 2024-09-23 12:49:35

首先,你的枚举变量名称对我来说似乎有点扭曲。 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 be AuthenticationResult 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.

伏妖词 2024-09-23 12:49:35

如果函数仅返回一个值,则应优先使用常规函数返回值而不是 outref 参数。

我想说这对于名为 IsXyz 的方法更为重要(重点是 is),并且以这种方式命名的方法应该返回 bool

If a function returns only one value then the regular function return value should be used in preference to an out or ref 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.

寻找一个思念的角度 2024-09-23 12:49:35

与返回值相比,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.

感情旳空白 2024-09-23 12:49:35

恕我直言,我不认为这是 bool 与 ref 的问题。对我来说,参考中返回的内容似乎是错误的。

我希望看到以下方法

public static bool TryAuthenticate(string userName, 
               string password, 
               string domainName, 
               out User user)

public static User Authenticate(string userName, 
               string password,                    
               string domainName)

一种是当您不关心原因时使用的方法,一种是您关心原因时使用的方法。例如,

这允许调用代码进行身份验证,然后在没有捕获的情况下执行某些操作

User u;
if (TryAuthenticate(user,pass,domain,ref u))
  //do somthing
else
  return;

,或者如果他们需要额外的信息,则使用捕获,

例如

User u;
try
{
    u = Authenticate(user,pass,domain);
    //do somthing
}
catch(AuthenticationError ae)
{
    switch (ae.Failure)
    {
        case AuthenticationFailure.AccountLocked:
            //dosomthing A
            break;
        case AuthenticationFailure.BadPassword:
            //dosomthing B
            break;
        case AuthenticationFailure.InvalidUser:
            //dosomthing c
            break;
        etc..

    }
}

现在,如果没有用户或令牌的概念,那么可能不需要尝试模式

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

public static bool TryAuthenticate(string userName, 
               string password, 
               string domainName, 
               out User user)

and

public static User Authenticate(string userName, 
               string password,                    
               string domainName)

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

User u;
if (TryAuthenticate(user,pass,domain,ref u))
  //do somthing
else
  return;

Or if they need that extra info then use a catch

e.g.

User u;
try
{
    u = Authenticate(user,pass,domain);
    //do somthing
}
catch(AuthenticationError ae)
{
    switch (ae.Failure)
    {
        case AuthenticationFailure.AccountLocked:
            //dosomthing A
            break;
        case AuthenticationFailure.BadPassword:
            //dosomthing B
            break;
        case AuthenticationFailure.InvalidUser:
            //dosomthing c
            break;
        etc..

    }
}

Now if there's no concept of a user or token then the Try Pattern probably isn't needed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文