2

I am developing an AI text communication engine, and I was wondering if anyone point me in the direction of a more efficient approach to validating user input other than just switch / if statements.

This is the foundation of it:

void Update(){
    string s = Console.Read()s.ToLower();

    if (s == "c1"){
        // do 1
    }
    else if (s == "c2"){
        // do 2
    }

    ...

    else if (s == "c9342"){
        // do 9342
    }
}

I should add, I have the ability to check for keywords in the sentence.

I feel like due to the fact that all input is strings, and it is dealing with language, this may be the only way to go, but if anyone has any better approach eg. interfaces, custom types, reflection, threading or anything then I am all ears.

Thanks, Andy

  • There are many ways. I might use a pre-polulated [ternary search tree](https://en.wikipedia.org/wiki/Ternary_search_tree). – 500 - Internal Server Error Jan 26 '21 at 20:37
  • 2
    If you need help refactoring the code, you need to provide real code, with real cases, not all, but minimal to see the schematic, to be able to help, or maybe use codereview.stackexchange.com. So here, it is wide/broad. Is the series c1, c2, c3, c4, c5...c9342 in increments of +1 ? Also what are do1, do2, do3... ? Methods ? Duplicate inner code with extractable parameters ? Or totally different processings ? Otherwise, in addition to if and switch, and the use of parameters to factorize in some methods and loops, you can also use dispatch tables like exposed by the answer of @AndreSantarosa. –  Jan 26 '21 at 20:49
  • 1
    To add to everyone else, don't keep doing `.ToLower()` just do `.Equals(...., StringComparison.OrdinalIgnoreCase)` – Charlieface Jan 26 '21 at 21:31

1 Answers1

1

Andy! You can work with delegates to achieve that flexibility. Delegates are a little complex and not as fast as a "direct" code, but they have their value.

Here I'm assuming that your comparison object will always be a string (and a lot of other stuff, if this solution doesn't fit your need, please leave a comment so we can work on that).

// Create a dictionary where the key is your comparison string and
// the action is the method you want to run when this condition is matched
Dictionary<string, Action> ifs = new Dictionary<string,Action>()
{
    // Note that after the method name you should not put () 
    // otherwise you would be invoking this method instead of create a "pointer" 
    {"c1", ExecuteC1},
    {"c2", ExecuteC2},
    {"c9342", ExecuteC9342},
}

private void ExecuteC1()
{
    Console.WriteLine("c1");
}    

private void ExecuteC2()
{
    Console.WriteLine("c2");
}    

private void ExecuteC9342()
{
    Console.WriteLine("c9342");
}

public RunCondition(string condition)
{
   // Get the condition related value by its key and calls the method with 'Invoke()'
   ifs[condition].Invoke();
}    
Andre.Santarosa
  • 1,150
  • 1
  • 9
  • 22
  • 1
    Thank you for recognizing my question as a valid question. I have had such horrible luck with stack overflow despite following all the rules that I stopped using it for years. delgates sound like great pathway. I appreciate the insight and really really appreciate that you took the time to help out a fellow programmer in search of help. I always leave this site so angry after asking a question and getting so many negative responses. So thank you. –  Jan 26 '21 at 20:50
  • 1
    [Dispatch table](https://en.wikipedia.org/wiki/Dispatch_table) –  Jan 26 '21 at 20:51
  • @Triangle4Studios By not answering questions to help you, you are depriving yourself of a potential for better and shorter answers. In some cases, less than 10 lines may replace hundreds of lines of code and even more. But if this answer matches your need, that's fine. Feel free to post any questions again if you have any problem. –  Jan 26 '21 at 20:58
  • Glad to help @Triangle4Studios =) Oh, if you need parameters or a return type go with Func instead of Action – Andre.Santarosa Jan 26 '21 at 21:01