函数中有很多if else's块.这些可以移除吗?

本文关键字:if else 函数 | 更新日期: 2023-09-27 18:12:53

我有一个函数需要根据提供的日期时间检查日期时间。我的功能如下所示(它工作得很好,但我不喜欢它)。唯一需要改变的是运算符,但目前我有几个if,否则if的etc &已复制的代码行数

我肯定我很笨,有更好的方法来做这件事吗?

 enum DateTimeOperator
 {
        Equals = 0, GreaterThanOrEqualTo, GreaterThan, LessThan
 }

函数

 bool DateChecked(DateTime dateCheck DateTimeOperator dateOperator)
 {
       if(dateOperator == DateTimeOperator.Equals)
       {
           if (File.GetLastWriteTime(filePath + fileName).Date == dateCheck .Date)
                        return true;
                    else
                        return false;
       }
       else if(dateOperator == DateTimeOperator.GreaterThanOrEqualTo)
       {
           if (File.GetLastWriteTime(filePath + fileName).Date >= dateCheck .Date)
                        return true;
                    else
                        return false;
       }
       else if(dateOperator == DateTimeOperator.LessThan)
       {
           if (File.GetLastWriteTime(filePath + fileName).Date < dateCheck .Date)
                        return true;
                    else
                        return false;
       }
 }

函数中有很多if else's块.这些可以移除吗?

没有if/switch只是为了好玩:

Dictionary<DateTimeOperator, Func<DateTime, DateTime, bool>> operatorComparer = new Dictionary<DateTimeOperator, Func<DateTime, DateTime, bool>>
{
    { DateTimeOperator.Equals, (a, b) => a == b },
    { DateTimeOperator.GreaterThanOrEqualTo, (a, b) => a >= b },
    { DateTimeOperator.GreaterThan, (a, b) => a > b },
    { DateTimeOperator.LessThan, (a, b) => a < b }
};
bool DateChecked(DateTime dateCheck, DateTimeOperator dateOperator)
{
   //TODO: add a sanity check
   return operatorComparer[dateOperator](File.GetLastWriteTime(filePath + fileName).Date, dateCheck .Date);
}

我建议使用扩展方法:当您想使用DateTimeOperator:

比较日期时,您不需要放置任何switchif
  public static class DateTimeOperatorExtensions {
    public static Func<Boolean, DateTime, DateTime> Comparison(this DateTimeOperator operation) {
      switch(operation) {
        //TODO: implenent other cases: i.e. DateTimeOperator.NotEquals here
        DateTimeOperator.Equals:
          return (left, right) => left == right;
        DateTimeOperator.GreaterThanOrEqualTo:
          return (left, right) => left >= right;  
        DateTimeOperator.LessThan:
          return (left, right) => left < right;   
        default: 
          return (left, right) => left == right; 
      }
    }
  }
  ...
  bool DateChecked(DateTime dateCheck DateTimeOperator dateOperator) {
    return dateOperator.Comparison()(dateCheck, File.GetLastWriteTime(filePath + fileName).Date);
  } 

这是我对上述代码的版本,我认为它更简单,更易于阅读

bool DateChecked(DateTime dateCheck DateTimeOperator dateOperator)
{
    var result = false;
    var myDate = File.GetLastWriteTime(filePath + fileName).Date;
    switch(dateOperator)
    {
        case DateTimeOperator.Equals:
            result = myDate == dateCheck.Date;
            break;
        case DateTimeOperator.GreaterThanOrEqualTo:
            result = myDate >= dateCheck.Date;
            break;
        case DateTimeOperator.LessThan:
            result =  myDate < dateCheck.Date;
            break;
    }
    return result;
}

或者如果你不喜欢一个返回语句

bool DateChecked(DateTime dateCheck DateTimeOperator dateOperator)
{
    var myDate = File.GetLastWriteTime(filePath + fileName).Date;
    switch(dateOperator)
    {
        case DateTimeOperator.Equals:
            return myDate == dateCheck.Date;
        case DateTimeOperator.GreaterThanOrEqualTo:
            return myDate >= dateCheck.Date;
        case DateTimeOperator.LessThan:
            return myDate < dateCheck.Date;
    }
}