C#Switch语句重构

时间:2009-11-25 15:26:05

标签: c# refactoring switch-statement

以下代码的目的是确定某个特定日期是否属于“周末”,即周四中午12:00之后,最少2天以及周一中午12:00之前

有更好的方法吗? If-Else变得丑陋,而战略模式对此来说太过分了。

public bool ValidateWeekend(DateTime pickupDate, DateTime dropoffDate)
    {
        TimeSpan ts = dropoffDate.Subtract(pickupDate);

        if (ts.TotalDays >= 2 && ts.TotalDays <= 4)
        {
            switch (pickupDate.DayOfWeek)
            {
                case DayOfWeek.Thursday:
                    if (pickupDate.Hour >= 12)
                    {
                        switch (dropoffDate.DayOfWeek)
                        {
                            case DayOfWeek.Sunday:
                                return true;
                            case DayOfWeek.Monday:
                                if (dropoffDate.Hour <= 12)
                                {
                                    return true;
                                }
                                return false;
                        }
                    }
                    break;
                case DayOfWeek.Friday:
                    switch (dropoffDate.DayOfWeek)
                    {
                        case DayOfWeek.Sunday:
                            return true;
                        case DayOfWeek.Monday:
                            if (dropoffDate.Hour <= 12)
                            {
                                return true;
                            }
                            return false;
                    }
                    break;
                case DayOfWeek.Saturday:
                    switch (dropoffDate.DayOfWeek)
                    {
                        case DayOfWeek.Sunday:
                            return true;
                        case DayOfWeek.Monday:
                            if (dropoffDate.Hour <= 12)
                            {
                                return true;
                            }
                            return false;
                    }
                    return false;
            }
        }
        return false;
    }

9 个答案:

答案 0 :(得分:6)

你绝对应该重构dropoffDate - 因为代码重复了3次!最简单的清理:我会介绍一个检查pickupDate的函数和另一个检查dropoffDate的函数:

private bool IsPickupWeekend(DateTime pickupDate)
{
    switch (pickupDate.DayOfWeek)
            {
                case DayOfWeek.Thursday:
                    return pickupDate.Hour >= 12;
                case DayOfWeek.Friday:                    
                case DayOfWeek.Saturday:
                    return true;
            }
        }
        return false;
}

private bool IsWeekendDropOff(DateTime dropoffDate)
{
    switch (dropoffDate.DayOfWeek)
                    {
                        case DayOfWeek.Sunday:
                            return true;
                        case DayOfWeek.Monday:
                            if (dropoffDate.Hour <= 12)
                            {
                                return true;
                            }
                            return false;
                    }
                    return false;

}

现在你的主要功能是2个班轮:

if (ts.TotalDays >= 2 && ts.TotalDays <= 4)
{
    return IsPickupWeekend(pickupDate) && IsWeekendDropOff(dropoffDate);
}

答案 1 :(得分:3)

我认为你可以在这里提取一个方法:

private bool ValidateDropoff(DateTime dropoffDate)
{
    switch (dropoffDate.DayOfWeek)
    {
        case DayOfWeek.Sunday:
           return true;
        case DayOfWeek.Monday:
           return dropoffDate.Hour <= 12;
        default:
           return false;
    }
}

答案 2 :(得分:1)

if (ts.TotalDays >= 2 && ts.TotalDays <= 4)
{
    var hour_limit = new Func<Boolean>(() => {
        switch (dropoffDate.DayOfWeek)
        {
            case DayOfWeek.Sunday:
                return true;
            case DayOfWeek.Monday:
                return dropoffDate.Hour <= 12;
            default:
                return false;
        }

    });

    switch (pickupDate.DayOfWeek)
    {
        case DayOfWeek.Thursday:
            if (pickupDate.Hour >= 12)  return hour_limit();
            break;
        case DayOfWeek.Friday:
        case DayOfWeek.Saturday:
            return hour_limit();
        default: 
            break;
    }
}

return false;

答案 3 :(得分:0)

不太清楚,但是你走了:

public bool ValidateWeekend(DateTime pickupDate, DateTime dropoffDate){
    TimeSpan ts = dropoffDate.Subtract(pickupDate);

    if (ts.TotalDays >= 2 && ts.TotalDays <= 4){
        switch (pickupDate.DayOfWeek){
            case DayOfWeek.Thursday:
                if (pickupDate.Hour >= 12){
                    switch (dropoffDate.DayOfWeek){
                        case DayOfWeek.Sunday:
                            return true;
                        case DayOfWeek.Monday:
                            return dropoffDate.Hour <= 12;
                    }
                }
                break;
            case DayOfWeek.Friday:
                switch (dropoffDate.DayOfWeek){
                    case DayOfWeek.Sunday:
                        return true;
                    case DayOfWeek.Monday:
                        return dropoffDate.Hour <= 12;
                }
                break;
            case DayOfWeek.Saturday:
                switch (dropoffDate.DayOfWeek){
                    case DayOfWeek.Sunday:
                        return true;
                    case DayOfWeek.Monday:
                        return dropoffDate.Hour <= 12;
                }
                return false;
        }
    }
    return false;
}

答案 4 :(得分:0)

我的第一次破解:

if (ts.TotalDays >= 2 && ts.TotalDays <= 4)
        {
            switch (pickupDate.DayOfWeek)
            {
                case DayOfWeek.Thursday:
                case DayOfWeek.Friday:
                case DayOfWeek.Saturday:
                    if (pickupDate.DayOfWeek == DayOfWeek.Thursday && pickupDate.Hour <= 12)
                        return false;

                    switch (dropoffDate.DayOfWeek)
                    {
                        case DayOfWeek.Sunday:
                            return true;
                        case DayOfWeek.Monday:
                            return dropoffDate.Hour <= 12;
                    }
                    return false;

                default:
                    return false;
            }
        }
        return false;

答案 5 :(得分:0)

在交换机中

尝试

retrun (dropoffDate.DayOfWeek == DayOfWeek.Sunday && dropoffDate.Hour <= 12 || dropoffDate.DayOfWeek == DayOfWeek.Sunday)

答案 6 :(得分:0)

我会这样做

public bool ValidateWeekend(DateTime pickupDate, DateTime dropoffDate)
{
    TimeSpan ts = dropoffDate.Subtract(pickupDate);

    if (ts.TotalDays >= 2 && ts.TotalDays <= 4)
    {
        switch (pickupDate.DayOfWeek)
        {
            case DayOfWeek.Thursday:
                if (pickupDate.Hour >= 12)
                {
                    reurn DayOfWeek(dropOffDate.DayOfWeek);
                }
                break;
            case DayOfWeek.Friday, DayOfWeek.Saturday:
                {
                    return DayOfWeek(dropOffDate.DayOfWeek);
                }
        }
    }
    return false;
}

public bool DayOfWeek(DateTime dropOffDate)
    {
switch (dropoffDate.DayOfWeek)
    {
        case DayOfWeek.Sunday:
            {
                return true;
            }
        case DayOfWeek.Monday:
            {
                if (dropoffDate.Hour <= 12)
                    {
                        return true;
                    }
                return false;
            }
       return false;
   }
 }

答案 7 :(得分:0)

这是我的抨击:

  /// <summary>
    /// Gets the weekend days.
    /// </summary>
    /// <returns></returns>
    public List<DayOfWeek> GetWeekendDays()
    {
        List<DayOfWeek> days = new List<DayOfWeek>()
                                   {
                                       DayOfWeek.Thursday,
                                       DayOfWeek.Friday,
                                       DayOfWeek.Sunday
                                   };
        return days;
    }

    /// <summary>
    /// Validates the weekend.
    /// </summary>
    /// <param name="pickupDate">The pickup date.</param>
    /// <param name="dropoffDate">The dropoff date.</param>
    /// <returns></returns>
    public bool ValidateWeekend(DateTime pickupDate, DateTime dropoffDate)
    {
        bool isValid = false;
        TimeSpan ts = dropoffDate.Subtract(pickupDate);

        if (ts.TotalDays >= 2 && ts.TotalDays <= 4)
        {
            List<DayOfWeek> days = GetWeekendDays();

            foreach (DayOfWeek day in days)
            {
                if(pickupDate.DayOfWeek == day)
                {
                   isValid = ValidateDropOff(dropoffDate);
                    break;
                }
            }
        }

        return isValid;
    }

    /// <summary>
    /// Validates the drop off.
    /// </summary>
    /// <param name="dropoffDate">The dropoff date.</param>
    /// <returns></returns>
    private static bool ValidateDropOff(DateTime dropoffDate)
    {
        bool isValidDropOff = (dropoffDate.DayOfWeek == DayOfWeek.Sunday);

        if(dropoffDate.DayOfWeek == DayOfWeek.Monday)
        {
            if (dropoffDate.Hour <= 12)
            {
                isValidDropOff = true;
            }
        }

        return isValidDropOff;
    }

答案 8 :(得分:0)

    private readonly TimeSpan Midday = new TimeSpan(12, 0, 0);

    public bool ValidateWeekend(DateTime pickupDate, DateTime dropoffDate)
    {
        TimeSpan lengthOfTrip = dropoffDate.Subtract(pickupDate);

        if (lengthOfTrip.TotalDays < 2 || lengthOfTrip.TotalDays > 4)
            return false;

        return IsPickupDateConsideredWeekend(pickupDate) && IsDropoffDateConsideredWeekend(dropoffDate);
    }

    private bool IsPickupDateConsideredWeekend(DateTime pickupdate)
    {
        if (pickupdate.DayOfWeek == DayOfWeek.Thursday && pickupdate.TimeOfDay > Midday)
            return true;
        return false;
    }

    private bool IsDropoffDateConsideredWeekend(DateTime dropoffDate)
    {
        if (dropoffDate.DayOfWeek == DayOfWeek.Monday && dropoffDate.TimeOfDay <= Midday)
            return true;
        return false;
    }