mardi 5 juillet 2016

Simplify long nested if statement workflow C#

This program is for validating Excel row data. If a if statement is false, I need to append a message to a string and so forth for N number of if statements. This doesn't work because as you can see if the first if statement fails, I would not proceed onto the the other if statements. Also later on, I'll need to handle 100+ columns, so I'm looking for a way to do this.

Is there any other way I can rewrite this, so it is more readable and less repetitive? I know I can just do a giant if (.... && ....) for validating all cells and individual if statements to append messages, but I was wondering if there is a another way to do this. I would like to preserve the order of the errors, but that is not as important. The result would be something like "facilityID is invalid, daysOfTheWeek is invalid"

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;
        }

Aucun commentaire:

Enregistrer un commentaire