重构if语句

本文关键字:语句 if 重构 | 更新日期: 2023-09-27 17:54:44

所以当我导入一个excel文档到我的项目。然后,如果电子表格中的任何列为空,我将使用if语句显示相应的错误消息。我的代码正在工作,但它看起来不整洁。是否有任何方式重构所有的if语句到一个循环中显示错误消息,可能是另一个方法来检查所有?checkIfColumnIsEmpty是一个bool方法,如果列为空则返回true

//if either column 0 and 1 are empty && column 2,3,4 and 5 are not
    if (!checkIfColumnisEmpty(r.ItemArray[0]) || !checkIfColumnisEmpty(r.ItemArray[1])
        && checkIfColumnisEmpty(r.ItemArray[2]) && checkIfColumnisEmpty(r.ItemArray[3])
         && checkIfColumnisEmpty(r.ItemArray[4]) && checkIfColumnisEmpty(r.ItemArray[5]))
    {
        if (checkIfColumnisEmpty(r.ItemArray[0]) && !checkIfColumnisEmpty(r.ItemArray[1]))
        {
            throw new ImportBOQException("Error importing document: First column is empty");
        }
        else if (!checkIfColumnisEmpty(r.ItemArray[0]) && checkIfColumnisEmpty(r.ItemArray[1]))
        {
            throw new ImportBOQException("Error importing document: Second column is empty");
        }
        else if (!checkIfColumnisEmpty(r.ItemArray[0]) && !checkIfColumnisEmpty(r.ItemArray[1]))
        {
            //all columns are valid so...
            Column0inSpreadsheet = r.ItemArray[0] as string;
            Column1inSpreadsheet = r.ItemArray[1] as string;
          //Other code which performs other operations, once the level as reached this far
        }
    }                                
    //if column 0 and 1 are NOT empty && Either column 2,3,4 or 5 is empty
    else if (checkIfColumnisEmpty(r.ItemArray[0]) && checkIfColumnisEmpty(r.ItemArray[1])
          || !checkIfColumnisEmpty(r.ItemArray[2]) || !checkIfColumnisEmpty(r.ItemArray[3])
           || !checkIfColumnisEmpty(r.ItemArray[4]) || !checkIfColumnisEmpty(r.ItemArray[5]))
    {
        if (checkIfColumnisEmpty(r.ItemArray[2]))
        {
            throw new ImportBOQException("Error importing document: Third column is empty");
        }
        else if (checkIfColumnisEmpty(r.ItemArray[3]))
        {
            throw new ImportBOQException("Error importing document: Fourth column is empty");
        }
        else if (checkIfColumnisEmpty(r.ItemArray[4]))
        {
            throw new ImportBOQException("Error importing document: Fifth column is empty");
        }
        else if (checkIfColumnisEmpty(r.ItemArray[5]))
        {
            throw new ImportBOQException("Error importing document: Sixth column is empty");
        }
        else
        //all columns are valid so...
        {   Column2inSpreadsheet = (r.ItemArray[2]) as string;
            Column3inSpreadsheet = (r.ItemArray[3]) as string;
            Column4inSpreadsheet = (r.ItemArray[4]) as string;
            Column5inSpreadsheet = (r.ItemArray[5]) as string;
            //Other code which performs other operations, once the level as reached this far
        }
    }
    else
    //other errors ot related to empty colums
    {
        throw new Exception("Error Uploading");
    }
}

重构if语句

如果您所需要的只是检查其中一列是否为"空"并获取它的索引,您可以使用以下命令:

var firstEmptyColumn = r.ItemArray
    .Select((f, i) => new { Field=f, Index=i })
    .FirstOrDefault(x => r.IsNull(x.Index) || x.Field.ToString().Trim().Length == 0);
if (firstEmptyColumn != null)
{
    string errorMsg = string.Format("Error importing document: {0} column is empty",
        firstEmptyColumn.Index);
    throw new ImportBOQException(errorMsg);
}

首先,将r转换为具有命名属性的类,而不是使用数组索引器,并从源代码初始化集合。其次,创建带有签名的助手方法bool VerifyColumn(字符串值,字符串propertyName)。这是一个基本的重构,也许你可以把你的逻辑从"天哪"变成"这太乱了,但我们可以改变它!"