jeudi 13 juillet 2017

cleaning up super gross arrow head anti-pattern in c#

im fairly new to coding and im looking to get rid of bad habits early and start writing clean and efficient code. i am working on a console application that references an API and i have a series of deeply nested if's (at points up to 10 levels deep!).

if (countryRes == CountryDes)
        {
            //staying in country
            try2:
            //display reasons for travel
            Console.WriteLine("What Best Describes Your Reason For Traveling?");
            Console.WriteLine(" ");
            Console.WriteLine("1. United States Resident traveling Inside the U.S.");
            Console.WriteLine("2. Visiting United States For Business or Pleasure.");
            Console.WriteLine("3. Immigrating to The Unites States.");
            Console.WriteLine("4. Student, Faculty Member or Scholar With a J-1, F-1, H-3, M-1, or Q-1 Visa.");
            Console.WriteLine(" ");
            var x = Console.ReadLine();
            Console.Clear();

            if (x == "1")
            {
                //US resident
                //first print
                loop.Loops1();
            }
            else if (x == "2")
            {
                try3:
                //visiting the US
                Console.WriteLine("What Type of Coverage Do You Need?");
                Console.WriteLine(" "); 
                Console.WriteLine("1. Medical voerage");
                Console.WriteLine("2. Trip Cancellation");
                var r = Console.ReadLine();
                Console.WriteLine(" ");
                Console.Clear();

                if (r == "1")
                {
                    //medical coverage
                    Console.WriteLine("What Type of Medical Coverage Do You Want?");
                    Console.WriteLine(" ");
                    Console.WriteLine("1. Scheduled benifits");
                    Console.WriteLine("2. Comprehensive Benifits");
                    var s = Console.ReadLine();
                    Console.WriteLine(" ");
                    Console.Clear();

                    if (s == "1")
                    {
                        //second print
                        loop.Loops1();
                    }
                    else if (s == "2")
                    {
                        //comprehensive benifits
                        //third print
                        loop.Loops1();
                    }
                    else
                    {
                        //first else
                        Console.WriteLine("Invalid Input. Please Try Again");
                    }
                }
                else if (r == "2")
                {
                    //trip canccelation
                    //fourth print
                    loop.Loops1();
                }
                else
                {
                    //secondelse
                    Console.WriteLine("Invalid Input. Please Try Again");
                    goto try3;
                }
            }
            else if (x == "3")
            {
                //immigration
                //fithprint
                loop.Loops1();
            }
            else if (x == "4")
            {
                //students...
                //sixthprint
                loop.Loops1();
            }
            else
            {
                //thirdelse
                Console.WriteLine("Invalid Input. Please try Again");
                goto try2;
            }

        }

this only a small sample of this gross nest of if's. i have done a lot of research on cleaning this up and am having a hard time understanding/using the answers i found. the biggest problem i have had with refactoring has been that each fallowing if is directly reliant on the if before it.

i also made a logic table of what input you need to reach each if. ill drop it in here in case its helpful: Excel table showing if paths

i would really appreciate some help, and an explanation of why your answer improves readability and efficiency would be excellent as well.

Aucun commentaire:

Enregistrer un commentaire