【问题标题】:Refactoring the code and developing a clean code重构代码并开发干净的代码
【发布时间】:2019-06-13 06:58:55
【问题描述】:

我有一个 dto 如下:

public class DaysDetails
{
 public bool Sun {get;set;}
 public bool Mon {get;set;}
 ...
 public bool Sat {get;set;} //All 7 days of the week
}

我有一个方法可以检查是否检查了日期并构建逗号分隔的字符串格式。例如:如果检查了星期日和星期一,则输出为“0,1”(对应于天的数字)

pubic string ConstructDays(DaysDetails d)
{
 StringBuilder constructDays = new StringBuilder();
 if(d.Sun == true)
  { 
    constructDays.Append("0");
  }
 if(d.Mon == true)
 {
   constructDays.Append("1");
  }
 ..... //So on for all seven days
 string day = Convert.toString(constructDays);

 if(day != string.Empty && day[0] == ",")
   day = day.Remove(0,1);
 return day;
}

我需要将此函数转换为更易于维护的代码和简化版本。这有什么可以改进的?

【问题讨论】:

  • 首先,您可以更改 DaysDetails 吗?我的意思是,为什么不使用带有标志的枚举?
  • 您可以使用列表而不是 CSV 字段
  • @anu:DayDetails 是我从 UI 映射字段中得到的。但是,是的,我可以使用从 DayDetails 派生的任何其他变量,并且可以将其传递给 ConstructDays 函数。最终,我需要一个逗号分隔的字符串,来自 DayDetails 或传递给函数的任何自定义变量。
  • 我投票结束这个问题,因为它要求代码审查尝试Code Review

标签: c# asp.net .net c#-4.0


【解决方案1】:

您可以通过将每个 bool 转换为 int 并加入生成的集合来简化代码。

public class DaysDetails
{
    public bool Sun { get; set; }
    public bool Mon { get; set; }
    public bool Sat { get; set; }
}

public string ConstructDays(DaysDetails d)
{
    var week = new[]
    {
        Convert.ToInt32(d.Sat),
        Convert.ToInt32(d.Sun),
        Convert.ToInt32(d.Mon),
    };
    return string.Join(",",  week);
}

或者,如果您寻找的不仅仅是 0/1:

public string ConstructDays(DaysDetails d)
{
    var week = new[]
    {
        d.Sat ? 0 : -1,
        d.Sun ? 1 : -1,
        d.Mon ? 2 : -1,
        //...//
    }.Where(x => x != -1);
    return string.Join(",",  week);
}

【讨论】:

    【解决方案2】:

    遍历类属性,如:

    pubic string ConstructDays(DaysDetails d)
    {
     int Idx = 0;
        string days = "";
        var obj = new DaysDetails ();
        foreach (var p in obj .GetType().GetProperties())
        {   days += (bool)p.GetValue(obj ) ? (days=="" ? Idx.ToString() : ","+Idx.ToString()) : "";
            Idx++;
        }
    return days
    }
    

    【讨论】:

      【解决方案3】:

      定义一个标志枚举来存储你的值:

      [Flags]
      public enum Days
      {
          None = 0,
          Sun = 1,  // 0
          Mon = 2,  // 1
          Tue = 4,  // 2
          Wed = 8,  // 3
          Thu = 16, // 4
          Fri = 32, // 5
          Sat = 64  // 6
      }
      

      您可以像这样设置选定的日期:

      var days = Days.None;
      
      if (some condition)
          days |= Days.Mon;
      
      if (some other condition)
          days |= Days.Wed;
      
      if (yet another condition)
          days |= Days.Sat;
      

      并根据设置的标志生成值,如下所示:

      static public string ConstructDays(Days days)
      {
          return string.Join(",", Enum.GetValues(typeof(Days))
                                      .Cast<Days>()
                                      .Where(d => days.HasFlag(d) && d != Days.None)
                                      .Select(d => Math.Log((int)d, 2)));  // 1,3,6
      }
      

      【讨论】:

        【解决方案4】:

        我建议两件事:创建一个单独的方法来将布尔值转换为 int 表示形式,并覆盖 ToString method 而不是生成单独的 ConstructDays 方法。

        public class DaysDetails
        {
            public bool Sun {get;set;}
            public bool Mon {get;set;}
            ...
            public bool Sat {get;set;} //All 7 days of the week
        
            public override string ToString() {
                //formatted string
                return $"{GetNumberRepresentationOfBool(Sun)},{GetNumberRepresentationOfBool(Mon)},{GetNumberRepresentationOfBool(Sat)}"
            }
        }
        
        public int GetNumberRepresentationOfBool(bool value) {
            return value ? 1 : 0
        }
        
        //printing the value
        Console.WriteLine(dayDetailsObject.ToString());
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-08-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-27
          • 2011-02-15
          相关资源
          最近更新 更多