【问题标题】:Refactor multiple if statments重构多个 if 语句
【发布时间】:2015-08-12 19:24:56
【问题描述】:

我有一个包含 14 个 if 语句的方法,我必须再做 12 次完全相同的事情,比如 160 个 if 语句。我如何重构以使这个更清洁?我正在使用 Telerik radgrid,并且我正在对单元格应用条件格式,但它对于每列都不同,并且根据不同列中的值而有所不同。这是我方法的开始。

仅供参考:它确实有效。

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        //Is it a GridDataItem
        if (e.Item is GridDataItem)
        {
            //Get the instance of the right type
            GridDataItem dataBoundItem = e.Item as GridDataItem;

            //Check the formatting condition
            if (dataBoundItem["sample_hour"].Text == "4hr YP")
            {
                if (float.Parse(dataBoundItem["ph"].Text) > 5.72 || float.Parse(dataBoundItem["ph"].Text) < 4.75)
                {
                    dataBoundItem["ph"].BackColor = Color.Yellow;
                    dataBoundItem["ph"].ForeColor = Color.Black;
                    dataBoundItem["ph"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["brix"].Text) > 22.36 || float.Parse(dataBoundItem["brix"].Text) < 17.35)
                {
                    dataBoundItem["brix"].BackColor = Color.Yellow;
                    dataBoundItem["brix"].ForeColor = Color.Black;
                    dataBoundItem["brix"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["temp"].Text) > 91 || float.Parse(dataBoundItem["temp"].Text) < 89)
                {
                    dataBoundItem["temp"].BackColor = Color.Yellow;
                    dataBoundItem["temp"].ForeColor = Color.Black;
                    dataBoundItem["temp"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["bud"].Text) > 41.76 || float.Parse(dataBoundItem["bud"].Text) < 3.121)
                {
                    dataBoundItem["bud"].BackColor = Color.Yellow;
                    dataBoundItem["bud"].ForeColor = Color.Black;
                    dataBoundItem["bud"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["cell_count"].Text) > 177.70 || float.Parse(dataBoundItem["cell_count"].Text) < 41.24)
                {
                    dataBoundItem["cell_count"].BackColor = Color.Yellow;
                    dataBoundItem["cell_count"].ForeColor = Color.Black;
                    dataBoundItem["cell_count"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["viability"].Text) > 69.183 || float.Parse(dataBoundItem["viability"].Text) < 5.65)
                {
                    dataBoundItem["viability"].BackColor = Color.Yellow;
                    dataBoundItem["viability"].ForeColor = Color.Black;
                    dataBoundItem["viability"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["dp4"].Text) > 10.892 || float.Parse(dataBoundItem["dp4"].Text) < 2.556)
                {
                    dataBoundItem["dp4"].BackColor = Color.Yellow;
                    dataBoundItem["dp4"].ForeColor = Color.Black;
                    dataBoundItem["dp4"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["dp3"].Text) > 6.052 || float.Parse(dataBoundItem["ph"].Text) < 1.412)
                {
                    dataBoundItem["bud"].BackColor = Color.Yellow;
                    dataBoundItem["bud"].ForeColor = Color.Black;
                    dataBoundItem["bud"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["maltose"].Text) > 8.285 || float.Parse(dataBoundItem["maltose"].Text) < .419)
                {
                    dataBoundItem["maltose"].BackColor = Color.Yellow;
                    dataBoundItem["maltose"].ForeColor = Color.Black;
                    dataBoundItem["maltose"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["glucose"].Text) > 6.695 || float.Parse(dataBoundItem["glucose"].Text) < -0.263)
                {
                    dataBoundItem["glucose"].BackColor = Color.Yellow;
                    dataBoundItem["glucose"].ForeColor = Color.Black;
                    dataBoundItem["glucose"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["lactic_acid"].Text) > .124 || float.Parse(dataBoundItem["lactic_acid"].Text) < .0101)
                {
                    dataBoundItem["lactic_acid"].BackColor = Color.Yellow;
                    dataBoundItem["lactic_acid"].ForeColor = Color.Black;
                    dataBoundItem["lactic_acid"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["glycerol"].Text) > .574 || float.Parse(dataBoundItem["ph"].Text) < .332)
                {
                    dataBoundItem["glycerol"].BackColor = Color.Yellow;
                    dataBoundItem["glycerol"].ForeColor = Color.Black;
                    dataBoundItem["glycerol"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["acetic_acid"].Text) > 0.176|| float.Parse(dataBoundItem["acetic_acid"].Text) < -.0756)
                {
                    dataBoundItem["acetic_acid"].BackColor = Color.Yellow;
                    dataBoundItem["acetic_acid"].ForeColor = Color.Black;
                    dataBoundItem["acetic_acid"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["ethanol"].Text) > 1.159 || float.Parse(dataBoundItem["ethanol"].Text) < .0053)
                {
                    dataBoundItem["ethanol"].BackColor = Color.Yellow;
                    dataBoundItem["ethanol"].ForeColor = Color.Black;
                    dataBoundItem["ethanol"].Font.Bold = true;

                }

            }

        }
    }

【问题讨论】:

  • 这是一个很好的代码审查问题:codereview.stackexchange.com

标签: c# asp.net datagridview telerik telerik-grid


【解决方案1】:

您所有的 if 语句都在做同样的事情,您可以将它们重构为一个方法。

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    //Is it a GridDataItem
    if (e.Item is GridDataItem)
    {
        //Get the instance of the right type
        GridDataItem dataBoundItem = e.Item as GridDataItem;

        //Check the formatting condition
        if (dataBoundItem["sample_hour"].Text == "4hr YP")
        {
            SetFormatting(dataBoundItem["ph"], 5.72, 4.75);
            SetFormatting(dataBoundItem["brix"], 22.36, 17.35);

            // etc...
        }

    }
}

private void SetFormatting(TableCell cell, float minValue, float maxValue)
{
    float value = float.Parse(cell.Text);

    if (value > minValue || value < maxValue)
    {
        cell.BackColor = Color.Yellow;
        cell.ForeColor = Color.Black;
        cell.Font.Bold = true;
    }
}

【讨论】:

  • 感谢您的作品完美干净得多。只是出于好奇,为什么要从实体模型中的类中获取最小值和最大值?
  • 我有这个实体类,最小值和最大值将是 val_hi 和 val_lo,并且有一行用于 ph、brix 等。 public partial class tbl_ferm_validation { public int validation_id { get;放; } 公共字符串 sample_type { 获取;放; } public Nullable val_hi { get;放; } public Nullable val_hihi { get;放; } public Nullable val_lo { get;放; } public Nullable val_lolo { get;放; } } }
  • 不是 100% 确定你在问什么。您可以从 e.Item.DataItem 属性中获取您的实体类。 var item = e.Item.DataItem as tbl_ferm_validation;
  • 早上好,我遇到了一个异常,浮点值 = float.Parse(cellText);输入字符串的格式不正确。输入是一个浮点数。 6.53。如果值为 null 会是什么情况?
  • 如果 cell.Text 为 null,您将收到 ArgumentNullException。在此处查看文档:link
【解决方案2】:

if 中的逻辑是相同的,所以 - 将其移到它自己的方法中:

private void Logic( GridDataItem dataBoundItem, string key, float max, float min )
{
    float f = float.Parse( dataBoundItem[key].Text );
    if( f > max || f < min )
    {
         dataBoundItem[key].BackColor = Color.Yellow;
         dataBoundItem[key].ForeColor = Color.Black;
         dataBoundItem[key].Font.Bold = true;
    }
}

然后像这样使用:

Logic( dataBoundItem, "ph", 5.72, 4.75 );
Logic( dataBoundItem, "brix", 22.36, 17.35 );
...

但是,您仍然将数据与逻辑混合在一起。因此,创建一个为您保存数据的类(即键、最大值、最小值等)。将该类的一堆实例添加到一个数组/列表中,并在每个实例上运行您的逻辑循环遍历它:

class Rule
{
    public string Key;
    public float Min, Max;
}

private Rule[] m_RulesCase1 = new Rule[]
                              {
                                  new Rule() { Key = "ph", Max = 5.72, Min = 4.75 }
                                  new Rule() { Key = "brix", Max = 22.36, Min = 17.35 }
                                  ...
                              };

private void ApplyRule( GridDataItem dataBoundItem, Rule r )
{
    float f = float.Parse( dataBoundItem[r.Key].Text );
    if( f > r.Max || f < r.Min )
    {
         dataBoundItem[r.Key].BackColor = Color.Yellow;
         dataBoundItem[r.Key].ForeColor = Color.Black;
         dataBoundItem[r.Key].Font.Bold = true;
    }
}

private void ApplyRules( GridDataItem dataBoundItem, IEnumerable<Rule> rules )
{
    foreach( var r in rules )
        ApplyRule( dataBoundItem, r );
}

而且,您总是可以更进一步,从配置文件或其他来源获取规则,避免将其完全包含在代码中。

【讨论】:

  • 我将如何从数据库中获取这些值?
  • 解释这需要的空间比我可以放入这些 cmets 的空间多得多..! Google 和 stackoverflow 搜索是您的朋友。
【解决方案3】:

你可以像这样创建一个过程:

private void CellFormat(string column,float min,float max,Color backcolor,Color forecolor,bool bold)
{
   if (float.Parse(dataBoundItem[column].Text) > min || float.Parse(dataBoundItem[column].Text) < max5)
   {
                    dataBoundItem[column].BackColor = backcolor;
                    dataBoundItem[column].ForeColor = forecolor;
                    dataBoundItem[column].Font.Bold = bold;
    }

}

会给什么:

CellFormat("ph"  , 4.75, 5.72, Color.Yellow,Color.Black,true) ;
CellFormat("brix",17.35,22.36, Color.Yellow,Color.Black,true) ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 2021-08-16
    相关资源
    最近更新 更多