vendredi 13 septembre 2019

Avoiding the if / switch case constructs to make a code more readable

I recently read a book about clean code, now Im trying to refactor some code that I wrote months ago. There are abstract device class and then some devices which are derived from it, like door, cardreader and so. I have an application that should be interactive via console. The question is if I can somehow refactor code bellow to avoid this long switches, which I use when parsing the command from the console. The user can add, remove, or modify the device, but every device and method has different number of attributes so I dont know if its possible to solve it with polymorphysm.

        string[] command = s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);


        if(command.Length == 0)
        {
            Console.WriteLine("Neplatny pozadavek");
            return;
        }
        switch (command[0])
        {
            case "add":
                if (command.Length < 3)
                {
                    Console.WriteLine("Neplatny pocet parametru pro operaci add");
                }
                else
                    AddCommand(command, r);
                break;
            case "modify":
                if (command.Length < 3)
                {
                    Console.WriteLine("Neplatny pocet parametru pro operaci modify");
                }
                else
                   ModifyCommand(command, r);
                break;
            case "remove":
                if (command.Length != 3)
                {
                    Console.WriteLine("Neplatny pocet parametru pro operaci remove");
                }
                else
                    RemoveCommand(command, r);
                break;
            case "move":
                if (command.Length != 3)
                {
                    Console.WriteLine("Neplatny pocet parametru pro operaci move");
                }
                else
                    MoveCommand(command, r);
                break;
            default:
                Console.WriteLine("Neznamy prikaz");
                break;
        }
    }

When I pass the operation parsing, I need to parse the device and for each device need to use the if else statement for checking it, the code bellow is just for two device. Is it possible to avoid these switch/if statements somehow to make the code more readable ? Also if I can parse string from console to match a enum better like using switch for all the enum values ? Thank u.

            if (command[2] == "door")
            {
                if (command.Length != 6)
                {
                     return;
                }
                State state;

                switch (command[5])
                {
                    case "locked":
                        state = State.Locked;
                        break;
                    case "open":
                        state = State.Open;
                        break;
                    case "openedforcibly":
                        state = State.OpenedForcibly;
                        break;
                    case "openfortoolong":
                        state = State.OpenForTooLong;
                        break;
                    default:
                        Console.WriteLine("Neplatny stav");
                        return;
                }

                Door d = new Door(deviceId, command[4], state);
                r.AddDeviceToGroup(id, d);
                return;
            }

            if (command[2] == "cardreader")
            {
                  if (command.Length != 6)
                  {
                        return;
                  }
                  CardReader d = new CardReader(deviceId, command[4], command[5], true);
                  r.AddDeviceToGroup(id, d);
                  return;
        }

Aucun commentaire:

Enregistrer un commentaire