2 Replies - 373 Views - Last Post: 09 November 2017 - 11:53 AM Rate Topic: -----

#1 A. Adam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • Posts: 5
  • Joined: 08-November 17

Can someone help me with som ideas to improve this code?

Posted 08 November 2017 - 04:21 PM

List<string> loggbok = new List<string> { };
            DateTime datum = DateTime.Now;
            Console.WriteLine(datum);
 
            string titel = "";
            string logg = "";
            string sparad = "";
 
            bool isRunning = true;
            while (isRunning)
                try
                {
                    {
                        Console.WriteLine("\n\t Välkommen till loggboken");
 
                        Console.WriteLine("\n\t[1] Skriv nytt inlägg i loggboken\n" +
                                          "\t[2] Sök inlägg i loggboken\n" +
                                          "\t[3] Skriv ut alla loggar" +
                                          "\t[4] Avsluta programmet");
                        Console.Write("\tVälj: ");
 
                        int menyVal = Convert.ToInt32(Console.ReadLine());
 
                        switch (menyVal)
                        {
                            case 1:
                                Console.Clear(); 
                                Console.Write("Skriv titel på ditt inlägg: ");
                                titel = Console.ReadLine();
                                Console.Write("Skriv ditt inlägg: ");
                                Console.WriteLine(datum);
                                Console.WriteLine(titel);     
                                Console.WriteLine(sparad);
                                sparad = Console.ReadLine();
                                Console.WriteLine("Alla dina inlägg är nu sparade!");
                                logg = datum + "\n" + titel + "\n" + sparad;
 
                                loggbok.Add(logg);
                                break;
 
                            case 2:
                                Console.Clear();
                                Console.WriteLine("Skriv in ett sökOrd");
 
                                var sökObjekt = Console.ReadLine();
 
                                var hittatItem = false;
                                foreach (var item in loggbok)
                                {
                                    if (item.Contains(sökObjekt))
                                    {
                                        hittatItem = true;
 
                                        Console.WriteLine(item);
                                    }
                                }
                                if (!hittatItem)
                                {
                                    Console.WriteLine("Felaktig inmatning...Vänligen försök igen!");
                                }
                                break;
 
                            case 3:
                                Console.Clear();
                                Console.WriteLine("HÄR ÄR ALLA DINA SPARADE INLOGG: ");
                                foreach (string loggboken in loggbok)
                                {
                                    Console.WriteLine(loggboken);
                                }
                                Console.WriteLine("Tryck på valfri knapp på tangentbordet för att gå" +
                                    " tillbaka till meny");
                                Console.ReadKey();
                                break;
 
                            default:
                                
                                break;
 
                           case 4:
                                return; 
                        }
                    }
                }
 
                catch
                {
                    Console.Clear();
                    Console.WriteLine("Nu blev det fel, gör ett val i menyn...");
                }
         }


Is This A Good Question/Topic? 0
  • +

Replies To: Can someone help me with som ideas to improve this code?

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5898
  • View blog
  • Posts: 20,136
  • Joined: 05-May 12

Re: Can someone help me with som ideas to improve this code?

Posted 08 November 2017 - 05:34 PM

When you have a function or run of code that exceeds 25 lines, this is a code smell that you need to follow the SRP.
Was This Post Helpful? 0
  • +
  • -

#3 aidenkael  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 59
  • View blog
  • Posts: 275
  • Joined: 22-October 13

Re: Can someone help me with som ideas to improve this code?

Posted 09 November 2017 - 11:53 AM

Like Skydiver said, I would make this multiple functions. Maybe one for the input, one for the logic, and one for output at least. Follow SOLID principles, in that each class/function/etc. should only ever do one specific thing. I would also look into better variable names. Remember, we don't pay per character. Be descriptive so that future programmers (including yourself) know what the variable is for!

SOLID Principles
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1