简化长嵌套if语句工作流程C#

时间:2016-07-05 18:56:18

标签: c# if-statement

此程序用于验证Excel行数据。 如果if语句为false,我需要将消息附加到字符串,依此类推N个if语句。这不起作用,因为你可以看到第一个if语句是否失败,我不会继续查看其他if语句。稍后,我还需要处理100多列,所以我正在寻找一种方法来实现这一目标。

还有其他方法可以重写这个,所以它更具可读性和重复性吗?我知道我可以做一个巨大的if(....&& ....)来验证所有单元格和if if语句来附加消息,但我想知道是否还有另一种方法可以做到这一点。我想保留错误的顺序,但这并不重要。结果类似于"facilityID is invalid, daysOfTheWeek is invalid"我还返回自定义Pair数据类型。

string facilityID, facilityDockDoorID, increment, hoursOfOperationId, updatedById, startTime, endTime, dockDoorServiceType, daysOfTheWeek;
            facilityID = row[0];
            facilityDockDoorID = row[1];
            increment = row[2];
            hoursOfOperationId = row[3];
            updatedById = row[4];
            startTime = row[5];
            endTime = row[6];
            dockDoorServiceType = row[7];
            daysOfTheWeek = row[8];

            string errorMessage = " is invalid";
            if (IsInt(facilityID))
            {
                if (IsInt(facilityDockDoorID))
                {
                    if (IsInt(increment))
                    {
                        if (IsInt(hoursOfOperationId))
                        {
                            if (IsInt(updatedById))
                            {
                                if (IsTime(startTime))
                                {
                                    if (IsTime(endTime))
                                    {
                                        if (IsValidDockDoorServiceType(dockDoorServiceType))
                                        {
                                            if (IsValidDayOfTheWeek(daysOfTheWeek))
                                            {
                                                isDataValid.First = true;
                                            }
                                            else
                                            {
                                                isDataValid.Second += "daysOfTheWeek" + errorMessage + ",";                                                
                                            }
                                        }
                                        else
                                        {
                                            isDataValid.Second += "dockDoorServiceType" + errorMessage + ",";

                                        }
                                    }
                                    else
                                    {
                                        isDataValid.Second += "endTime" + errorMessage + ",";
                                    }
                                }
                                else
                                {
                                    isDataValid.Second += "startTime" + errorMessage + ",";
                                }
                            }
                            else
                            {
                                isDataValid.Second += "updatedById" + errorMessage + ",";
                            }
                        }
                        else
                        {
                            isDataValid.Second += "hoursOfOperationId" + errorMessage + ",";
                        }
                    }
                    else
                    {
                        isDataValid.Second += "increment" + errorMessage + ",";
                    }
                }
                else
                {
                    isDataValid.Second += "facilityDockDoorID" + errorMessage + ",";
                }
            }
            else
            {
                isDataValid.Second = "facilityID" + errorMessage + ",";
            }
            return isDataValid;
        }

4 个答案:

答案 0 :(得分:5)

如果您反转if语句并在每个语句中返回,则可以像这样展开代码。

if (!IsInt(facilityID))
{
    isDataValid.Second = "facilityID" + errorMessage + ",";
    return isDataValid;
}
if (!IsInt(facilityDockDoorID))
{
    isDataValid.Second += "facilityDockDoorID" + errorMessage + ",";
    return isDataValid;
}
if (!IsInt(increment))
{
    isDataValid.Second += "increment" + errorMessage + ",";
    return isDataValid;
}
if (!IsInt(hoursOfOperationId))
{
    isDataValid.Second += "hoursOfOperationId" + errorMessage + ",";
    return isDataValid;
}
if (!IsInt(updatedById))
{
    isDataValid.Second += "updatedById" + errorMessage + ",";
    return isDataValid;
}
if (!IsTime(startTime))
{
    isDataValid.Second += "startTime" + errorMessage + ",";
    return isDataValid;
}
if (!IsTime(endTime))
{
    isDataValid.Second += "endTime" + errorMessage + ",";
    return isDataValid;
}
if (!IsValidDockDoorServiceType(dockDoorServiceType))
{
    isDataValid.Second += "dockDoorServiceType" + errorMessage + ",";
    return isDataValid;
}
if (IsValidDayOfTheWeek(daysOfTheWeek))
{
    isDataValid.First = true;
}
else
{
    isDataValid.Second += "daysOfTheWeek" + errorMessage + ",";
}
return isDataValid;

但是,基于您与Second连接的事实,您更有可能想要像

这样的事情。
if (!IsInt(facilityID))
    isDataValid.Second = "facilityID" + errorMessage + ",";
if (!IsInt(facilityDockDoorID))
    isDataValid.Second += "facilityDockDoorID" + errorMessage + ",";
if (!IsInt(increment))
    isDataValid.Second += "increment" + errorMessage + ",";
if (!IsInt(hoursOfOperationId))
    isDataValid.Second += "hoursOfOperationId" + errorMessage + ",";
if (!IsInt(updatedById))
    isDataValid.Second += "updatedById" + errorMessage + ",";
if (!IsTime(startTime))
    isDataValid.Second += "startTime" + errorMessage + ",";
if (!IsTime(endTime))
    isDataValid.Second += "endTime" + errorMessage + ",";
if (!IsValidDockDoorServiceType(dockDoorServiceType))
    isDataValid.Second += "dockDoorServiceType" + errorMessage + ",";
if (!IsValidDayOfTheWeek(daysOfTheWeek))
    isDataValid.Second += "daysOfTheWeek" + errorMessage + ",";
isDataValid.First = isDataValid.Second.Length == 0;
return isDataValid;

请注意,我正在比较Second的长度,以确定是否发生任何错误。

答案 1 :(得分:4)

您可以反转if语句。例如:

if(!IsInt(facilityID))
{
    isDataValid.Second = "facilityID" + errorMessage + ",";
}
if(!IsInt(facilityDockDoorID))
{
    isDataValid.Second += "facilityDockDoorID" + errorMessage + ",";
}

答案 2 :(得分:2)

您可以构建验证器函数的集合,例如:

List<Func<string[], StringBuilder, bool>> validators = new List<Func<string[], StringBuilder, bool>>();

validators.Add((row, logger) => 
{
    string facilityID = row[0];

    if(IsInt(facilityID))
    {
        logger.AppendLine("facilityID is invalid");

        return false;
    }

    return true;
});

validators.Add((row, logger) =>
{
    string increment = row[2];

    if (IsInt(increment))
    {
        logger.AppendLine("increment is invalid");

        return false;
    }

    return true;
});

. . .

然后你可以像这样循环遍历:

StringBuilder log = new StringBuilder();

if(validators.Any(v => v(rows, log) == false))
{
    return false;
}
else
{
    return true;
}

isDataValid.Second = log.ToString();

通过这种方式,您可以将验证器逻辑分解为更小的块。这样可以避免嵌套if语句。每个验证器负责选择要验证的数据,返回有效/无效bool,并将任何消息记录到公共StringBuilder。随着验证器列表的增长,您只需向集合中添加一个新函数即可。

答案 3 :(得分:0)

最好将您的架构与代码功能区分开来。 这使您可以在将来更改架构,而无需更改其余代码。

    public class SchemaItem
    {
        public int Index;
        public string ColumnName;
        public int ParsedValue;

        public SchemaItem(int index, string columnName)
        {
            Index = index;
            ColumnName = columnName;
        }
    }
    public static Dictionary<int, SchemaItem> Schema = new Dictionary<int, SchemaItem>
    {
        {0, new SchemaItem(0, "facilityID")},
        {1, new SchemaItem(1, "facilityDockDoorID")},
        {2, new SchemaItem(2, "increment")},
        {3, new SchemaItem(3, "hoursOfOperationId")},
        {4, new SchemaItem(4, "updatedById")},
        {5, new SchemaItem(5, "startTime")},
        {6, new SchemaItem(6, "endTime")},
        {7, new SchemaItem(7, "dockDoorServiceType")},
        {8, new SchemaItem(8, "daysOfTheWeek")},
    };
    private static bool ParseIntAndOrWriteFailMessage(string[] row, StringBuilder logger, int index)
    {
        string cellContents = row[index];
        string columnName = Schema[index].ColumnName;
        int intContents = -1;
        if (int.TryParse(cellContents, out intContents))
        {
            Schema[index].ParsedValue = intContents;
            return true;
        }
        logger.AppendFormat("{0} is invalid,", columnName);
        return false;
    }

然后你最终会这样做:

        StringBuilder logger = new StringBuilder();
        foreach (var column in Schema.Select(pair => pair))
        {
            ParseIntAndOrWriteFailMessage(row, logger, column.Key);
        }
        // Parsed values are now stored in the Schema Dictionary (which probably could use a better name)