如何使多条件的if语句更短?

时间:2016-04-29 15:04:54

标签: c# .net

我开始修复客户的签名无法正确显示的问题。客户有2列,一列用于当前和之前的时间 - 因此他们可以有五个结果:

Current new  These two we just need to show the CurrDate
Prior New      This hits the 1st ret

Current New    This hits the 3rd ret
Prior Null

Current old  These three we need to show the PrevDate
Prior old      This hits the 2nd ret

Current new    This hits the 2nd ret
Prior old

Current old    This hits the 4th ret
Prior null

我得到了这个if语句

    private string CheckBoxDescription()
    {
        if (ViewState["PrevDate"] != null &&
            ViewState["PrevDate"].ToString().Trim() != "" && 
            ViewState["CurrDate"] != null && 
            ViewState["CurrDate"].ToString().Trim() != "")
        {
            if (DateTime.Parse(ViewState["PrevDate"].ToString()) > 
                   DateTime.Parse("01-JAN-12") &&
                DateTime.Parse(ViewState["CurrDate"].ToString()) >
                  DateTime.Parse("01-JAN-12"))
            {
                return "SELECT Statement to get CurrDate;
            }
            else
            {
                return "SELECT Statement to get PrevDate;
            }
        }

        if (DateTime.Parse(ViewState["CurrDate"].ToString()) >
            DateTime.Parse("01-JAN-12"))
        {
            return "SELECT Statement to get CurrDate (Same as the CurrDate above);
        }
        else
        {
            return "SELECT Statement to get PrevDate (Same as the PrevDate above);
        }
    }

我试过了:

    if (DateTime.Parse(ViewState["CurrDate"].ToString()) > 
          (DateTime.Parse("011-JAN-12")) &
       (DateTime.Parse(ViewState["PrevDate"].ToString()) > 
          (DateTime.Parse("011-JAN-12")) | 
       (DateTime.Parse(ViewState["PrevDate"].ToString()) !=
           null)))

但它不会获取其中一个空值。

5 个答案:

答案 0 :(得分:3)

对于空/空检查,您可以使用String.IsNullOrWhitespace

if (!string.IsNullOrWhiteSpace(ViewState["PrevDate"]) &&
    !string.IsNullOrWhiteSpace(ViewState["CurrDate"]))

我还会将解析后的日期存储在变量中,而不是每次都解析

DateTime minDate  = new DateTime(2012,1,1);
DateTime prevDate = DateTime.Parse(ViewState["PrevDate"]));
DateTime currDate = DateTime.Parse(ViewState["CurrDate"]));

然后在比较中使用那些:

if (prevDate > minDate &&
    currDate > minDate)

对于上次检查,DateTime.Parse 永远不会返回null,因此我不确定您在那里检查的条件是什么。如果 string 值可以为null,则只需检查:

if (currDate > minDate &
       (ViewState["PrevDate"] == null ||
        prevDate > minDate)
   )

答案 1 :(得分:0)

 private string CheckBoxDescription()
{
    if (ViewState["PrevDate"] != null &&
        ViewState["PrevDate"].ToString().Trim() != "" && 
        ViewState["CurrDate"] != null && 
        ViewState["CurrDate"].ToString().Trim() != "")
    {
        if (DateTime.Parse(ViewState["PrevDate"].ToString()) > 
               DateTime.Parse("01-JAN-12") &&
            DateTime.Parse(ViewState["CurrDate"].ToString()) >
              DateTime.Parse("01-JAN-12"))
        {
            return "SELECT Statement to get CurrDate;
        }
            return "SELECT Statement to get PrevDate;
    }

    if (DateTime.Parse(ViewState["CurrDate"].ToString()) >
        DateTime.Parse("01-JAN-12"))
    {
        return "SELECT Statement to get CurrDate (Same as the CurrDate above);
    }
        return "SELECT Statement to get PrevDate (Same as the PrevDate above);
}

从你在第二段代码中所做的,这是我可以改变的,不多但我所做的是删除else语句,因为你返回我的第一个if语句,else不需要。

答案 2 :(得分:0)

你的朋友:案例陈述https://msdn.microsoft.com/en-GB/library/06tc147t.aspx

多个if语句也有减慢代码速度的习惯,因为IDE必须处理所有条件

使用Case语句,它只会根据条件使用某些信息

答案 3 :(得分:0)

通过编写将字符串解析为可为空的日期的方法,可以大大简化这一过程:

private static DateTime? parseDate(object date)
{
    DateTime result;

    if (DateTime.TryParse(date.ToString(), out result))
        return result;

    return null;
}

请注意,我必须将参数设为object,因为我不知道ViewState["key"]返回的是什么类型。

您可以使用它来简化代码。但是,这样做表明您没有处理currDate不是有效日期的情况:

private string CheckBoxDescription()
{
    var prevDate = parseDate(ViewState["PrevDate"]);
    var currDate = parseDate(ViewState["CurrDate"]);

    if (prevDate != null && currDate != null)
    {
        return 
            prevDate > DateTime.Parse("01-JAN-12") && currDate > DateTime.Parse("01-JAN-12") 
            ? "SELECT Statement to get CurrDate" 
            : "SELECT Statement to get PrevDate";
    }

    // You have a problem here. What if currDate is not a valid date?

    if (currDate == null)
        throw new InvalidOperationException("Your current code doesn't handle this case.");

    if (currDate > DateTime.Parse("01-JAN-12"))
    {
        return "SELECT Statement to get CurrDate (Same as the CurrDate above)";
    }
    else
    {
        return "SELECT Statement to get PrevDate (Same as the PrevDate above)";
    }
}

答案 4 :(得分:0)

这个怎么样;

private string CheckBoxDescription()
{
    var prevDate = ViewState["PrevDate"] != null && ViewState["PrevDate"].ToString().Trim() != ""
    ? DateTime.Parse(ViewState["PrevDate"].ToString()) 
    : null;
    var currDate = ViewState["PrevDate"] != null && ViewState["CurrDate"].ToString().Trim() != "" 
    ? DateTime.Parse(ViewState["CurrDate"].ToString()) 
    : null;

    if (prevDate != null && currDate != null)
    {

       return 
        prevDate > DateTime.Parse("01-JAN-12") && currDate > DateTime.Parse("01-JAN-12") 
        ? "SELECT Statement to get CurrDate" 
        : "SELECT Statement to get PrevDate";
    }

    if (currDate > DateTime.Parse("01-JAN-12"))
    {
        return "SELECT Statement to get CurrDate (Same as the CurrDate above);
    }
    else
    {
        return "SELECT Statement to get PrevDate (Same as the PrevDate above);
    }
}