Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to write multiple if else conditions using best practices. Here is my code.
C#
<pre>public class HomeController : Controller
    {
        //Creating object of Result class
        Result result = new Result();

        // GET: Home
        public async Task<ActionResult> Index(string key, string value)
        {
            DBConnection dbConnection = new DBConnection();
            dbConnection.MakeConnection();
            try
            {
                dbConnection.oracleCommand.CommandText = "QUERY";
                OracleDataReader Reader = dbConnection.oracleCommand.ExecuteReader();
                Reader.Read();
                count = Reader.GetInt16(0);

                for (int i = 0; i < count; i++)
                {
                    //Updating records
                    dbConnection.oracleCommand.CommandText = "QUERY";
                    dbConnection.oracleCommand.ExecuteNonQuery();

                    dbConnection.oracleCommand.CommandText = "QUERY";
                    OracleDataReader Reader2 = dbConnection.oracleCommand.ExecuteReader();

                    while (Reader2.Read())
                    {
                        //Getting keyname and keyvalue
                        keyName = (Reader2.GetValue(1)).ToString();
                        keyValue = (Reader2.GetValue(2)).ToString();
                    }

                    dbConnection.oracleCommand.CommandText = "QUERY";
                    OracleDataReader Reader3 = dbConnection.oracleCommand.ExecuteReader();
                    while (Reader3.Read())
                    {
                        if ((!Reader3.GetValue(1).Equals(System.DBNull.Value)))
                        {
                            //Checking api type
                            if ((Reader3.GetValue(1).ToString() == ("REST")))
                            {
                                dbConnection.oracleCommand.CommandText = "QUERY";
                                OracleDataReader Reader4 = dbConnection.oracleCommand.ExecuteReader();

                                while (Reader4.Read())
                                {

                                    if ((!Reader4.GetValue(0).Equals(System.DBNull.Value)))
                                    {
                                        //Getting url
                                        url = Reader4.GetValue(1).ToString();
                                        //Checking whether a get or a post
                                        if ((Reader4.GetValue(0).ToString() == ("GET")))
                                        {
                                            RestCall restCall = new RestCall();
                                            await restCall.RunAsync(keyName, keyValue);

                                        }
                                    }
                                    else if ((Reader4.GetValue(0).ToString() == ("POST")))
                                    {
                                        //"Do a rest call for post method.";
                                        RestCall restCall = new RestCall();
                                        await restCall.RunAsync(keyName, keyValue);
                                    }
                                    else
                                    {
                                        //"Not a get or a post";
                                        errorMessage = "Error";
                                    }
                                }
                            }
                        }
                        else
                        {
                            //"A null value.";
                            errorMessageNull = "A null value.";
                        }
                    }

                    //Soap
                    if ((!Reader3.GetValue(1).Equals(System.DBNull.Value)))
                    {
                        if ((Reader3.GetValue(1).ToString() == ("SOAP")))
                        {
                            dbConnection.oracleCommand.CommandText = "QUERY";
                            OracleDataReader Reader4 = dbConnection.oracleCommand.ExecuteReader();
                            while (Reader4.Read())
                            {

                                if ((!Reader4.GetValue(0).Equals(System.DBNull.Value)))
                                {
                                    //Getting url
                                    url = Reader4.GetValue(1).ToString();
                                    //Read function
                                    if ((Reader4.GetValue(0).ToString() == ("READ")))
                                    {
                                        //SoapCall soapCall = new SoapCall();
                                        //soapCall.DoSoapCall();
                                    }
                                }
                                //Create function
                                else if ((Reader4.GetValue(0).ToString() == ("CREATE")))
                                {
                                    //"Do a soap call for post method.";
                                }
                                else
                                {
                                    //"Not a get or a post";
                                    errorMessage = "Error";
                                }
                            }

                        }
                        else
                        {
                            //"Not a get or a post";
                            errorMessage = "Error";
                        }

                    }
                    else
                    {
                        //"Not a get or a post";
                        errorMessage = "Error";
                    }
                    Reader.Dispose();
                    Reader3.Dispose();
                    dbConnection.CloseConnection();
                }
            }
            catch (Exception e)
            {
                exception = e.Message.ToString();
            }

I want to execute these queries if they meet certain conditions. After that I want to do rest and soap calls. Is there any method to write this code without using multiple if else conditions. I mean a more readable way.

What I have tried:

I did not have an idea of another method.
Posted
Updated 14-Mar-18 23:55pm

1 solution

You can get rid of immediate nesting like this

if (conditionA)
{
    if (conditionB)
    {
        DoSomething();
    }
}


but putting all conditions in the same if

if (conditionA && conditionB)
{
    DoSomething();
}


For the code that processes the reader you could just put that in its own function as pass the reader in as a param.

if (conditionA && conditionB)
{
    ProcessReader(Reader4);
}


If you have too much nesting still then repeat the process of putting some of the nested code in a function which you then call.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900