重构代码以使用较少的if语句

时间:2017-07-31 16:19:56

标签: c# winforms refactoring

我只是想知道重构这个语句的最佳方法是什么才能使用更少的condidtions?如果somone可以指出我正确的方向,我会非常感激地试图清理这个声明而没有相同的功能

try
{
    var errorProviders = new List<ErrorProvider>() { epEmail, epAlternative, epMobile, epTown, epLandline, epHouseName, epForeName, epSurname, epPostcode, epCountry, epHouseName, epLocality, epCounty };

    foreach (Control c in panel1.Controls)
    {
        if (c is SpellBox || c is TextBox)
        {
            if (!string.IsNullOrWhiteSpace(txt_ForeName.Text) | !string.IsNullOrWhiteSpace(txt_SurName.Text))
            {
                if (cmb_Title.SelectedIndex != -1)
                {
                    if (cmb_PrefConTime.SelectedIndex != -1)
                    {
                        if (isPhoneNumber())
                        {
                            if (errorProviders.Any(e => e.GetError(c).Length > 0))
                            { 
                                return false; 
                            }
                        }
                        else
                        {   
                            epPrefConNumber.SetError(cmb_PrefConNumber, "Error");
                            return false; 
                        }
                    }
                    else
                    {   
                        epPrefConTime.SetError(cmb_PrefConTime, "Error in: prefered contact time feild");
                        return false; 
                    } 
                }
                else
                {   
                    epTitle.SetError(cmb_Title, "Title"); 
                    return false;
                } 
            }
            else
            { 
                epBothNames.SetError(txt_SurName, "Error:"); 
                epBothNames.SetError(txt_ForeName, "Error:");
                return false;
            }
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString())+ "Error has occurred, Please cancel and try again!");
}
return true;

如果有办法使用裸最小条件来减少代码?

1 个答案:

答案 0 :(得分:1)

如果你必须检查所有这些条件,那么你必须检查所有这些条件。在我看来,你最令人震惊的问题是深度嵌套和验证字段,这些字段与循环内的控件完全无关。您可以通过反转if条件并提前返回来修复嵌套。其余的,只需将不相关的验证移到循环外。

try
{
    var errorProviders = new List<ErrorProvider>() { epEmail, epAlternative, epMobile, epTown, epLandline, epHouseName, epForeName, epSurname, epPostcode, epCountry, epHouseName, epLocality, epCounty };

    if (string.IsNullOrWhiteSpace(txt_ForeName.Text) && string.IsNullOrWhiteSpace(txt_SurName.Text))
    {
        epBothNames.SetError(txt_SurName, "Error:"); 
        epBothNames.SetError(txt_ForeName, "Error:");
        return false;
    }

    if (cmb_Title.SelectedIndex == -1)
    {   
        epTitle.SetError(cmb_Title, "Title"); 
        return false; 
    }

    if (cmb_PrefConTime.SelectedIndex == -1)
    {   
        epPrefConTime.SetError(cmb_PrefConTime, "Error in: prefered contact time feild");
        return false; 
    }

    if (!isPhoneNumber())
    {   
        epPrefConNumber.SetError(cmb_PrefConNumber, "Error");
        return false; 
    }

    foreach (Control c in panel1.Controls.Where(x => x is SpellBox || x is TextBox))
    {
        if (!errorProviders.Any(e => e.GetError(c).Length > 0))
        { 
            return false; 
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString())+ "Error has occurred, Please cancel and try again!");
}
return true;