使用返回的错误信息,以确定是否存在异常 [英] Using a returned error message to determine if error is present

查看:155
本文介绍了使用返回的错误信息,以确定是否存在异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一个关于采取只有一个含义返回值哥们说话。在我以前的工作中,我们曾与C ++,并Typedef的wBOOL让一个0是wFALSE,1例wTRUE。建筑师说,我们也可以返回2,3,4 ...了解更多信息,我认为这是一个可怕的想法。如果我们期望wTRUE = 1和wFALSE = 0和wBOOL = {wTRUE,wFALSE},返回别的应避免......现在,让今天的C#。

I was recently talking with a buddy about return values taking only a single meaning. At my previous job, we worked with C++ and had typedef'ed wBOOL so that a 0 was wFALSE, and 1 was wTRUE. The architect said that we can also return 2, 3, 4... for more information, which I think is a horrible idea. If we expect wTRUE = 1 and wFALSE = 0 and wBOOL = {wTRUE, wFALSE}, returning anything else should be avoided... now, on to today's C#.

我最近审查了一段代码,其中还有的是决定如果有一个错误,并且返回的字符串返回给用户的功能集合:

I recently reviewed a piece of code where there were a collection of functions that determined if there was an error and returned the string back to the user:

private bool IsTestReady(out string errorMessage)
{
  bool isReady = true;
  errorMessage = string.Empty;
  if(FailureCondition1)
  {
    isReady = false;
    errorMessage = FailureMessage1;
  }
  else if(FailureCondition2)
  {
    isReady = false;
    errorMessage = FailureMessage2;
  }
  //... other conditions
  return isReady;
}



然后,要使用这些功能...

Then, to use these functions...

private enum Tests
{ TestA, TestB, TestC }
private void UpdateUI()
{
  string error = string.Empty;
  bool isTestReady;
  switch(this.runningTest) // which test are we running (TestA, TestB, or TestC)
  {
    case Tests.TestA:
      isTestReady = IsTestAReady(error);
      break;
    case Tests.TestB:
      isTestReady = IsTestBReady(error);
      break;
    case Tests.TestC:
      isTestReady = IsTestCReady(error);
      break;
  }
  runTestButton.Enabled = isTestReady;
  runTestLabel.Text = error;
}



我想这些了分成两种方法:

I thought to separate these out into two methods:

private string GetTestAErrorMessage()
{
  //same as IsTestReady, but only returns the error string, no boolean stuffs
}

private bool IsTestAReady
{
  get{ return string.IsNullOrEmpty(GetTestAErrorMessage()); }
}



这是否违反没有返回值平均超过一个的主要事情?例如,在这种情况下,如果有错误消息IsNullOrEmpty,那么就没有误差。我认为这并不违反该负责人;我共同工作过一样。对我来说,没有比这种不同的:

Does this violate the principal of not having a return value mean more than one thing? For instance, in this case, if there error message IsNullOrEmpty, then there is no error. I think that this does not violate that principal; my co-worked does. To me, it's no different than this:

class Person
{
  public int Height {get;}
  public bool IsTall() { return Height > 10; }
}

这是一种不同的方法对这个问题的任何想法或建议?我想出来的参数是最糟糕的解决方案。

Any thoughts or suggestions on a different approach to this issue? I think the out parameter is the worst of the solutions.

推荐答案

我不具有null或空的忠实粉丝返回值表明,没有什么是错的。比你给一个更好的比较是:

I'm not a big fan of having the null or empty return value indicate that nothing is wrong. A better comparison than the one you gave is:

class Person
{
    public int Height {get;}
    public bool IsBorn() { return Height > 0; }
}

在.NET中,它是使用布尔与返回常见的做法出你在原来的方法见参数的格局(见各的TryParse 方法,例如)。但是,如果你喜欢,另一种解决方案是创建一个 TestReadyCheck 类布尔值和字符串作为属性都。我已经做了下面的类类似的东西,并一直与它很高兴。

In .NET, it is common practice to use the "bool return with out parameter" pattern you see in your original method (see the various TryParse methods, for example). However, if you prefer, another solution would be to create a TestReadyCheck class with both the boolean and the string as properties. I've done something similar with the following class, and been quite happy with it.

public class RequestFilterResult
{
    public static readonly RequestFilterResult Allow = new RequestFilterResult(true, null);
    public static RequestFilterResult Deny(string reason) { return new RequestFilterResult(false, reason); }
    protected RequestFilterResult(bool allowRequest, string denialReason)
    {
        AllowRequest = allowRequest;
        DenialReason = denialReason;
    }

    public bool AllowRequest { get; private set; }
    public string DenialReason { get; private set; }
}

这允许以下用法:

public RequestFilterResult Filter(...)
{
    if (FailureCondition1) return RequestFilterResult.Deny(FailureMessage1);
    if (FailureCondition2) return RequestFilterResult.Deny(FailureMessage2);
    return RequestFilterResult.Allow();
}



它的简洁,同时实施了失败的结果提供了一个失败消息,和成功的结果没有。

It's concise, while enforcing that failure results provide a failure message, and success results don't.

在一个侧面说明,你的开关的结构语句感觉就像一个代码味道对我来说。您可能要考虑如何利用多态。也许让每个测试有它自己的类,上面有一个 IsTestReady 方法?

On a side note, the structure of your switch statement feels like a code smell to me. You may want to consider ways to leverage polymorphism. Maybe make each test have its own class, with an IsTestReady method on it?

这篇关于使用返回的错误信息,以确定是否存在异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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