Search code examples
c#stringiif-function

Writing Dynamic IF Functions by user entry


I'm still pretty new with coding(C#) and am writing a file reader based on user criteria(What string they want to search).

What my program does:

The user can enter search a sting / multiple strings (with AND OR functionality) The program interprets the user entry and re-writes the string into code. e.g.

string fileLine = File.ReadAllText(line);

USER:

(hi AND no) OR yes

PROGRAM:

if((fileLine.Contains(hi) && fileLine.Contains(no)) || fileLine.Contains(yes))

What I'm trying to do:

When matching the string to the File string i use an IF Function:

if(fileLine.Contains(hi))
{
   //do A LOT of stuff here.
}

My first idea was to make a string out of the the entered string and replace the "condition" in the IF Function.

Am i going about this in the wrong way? What would the best way of achieving this be?


Solution

  • Parameterizing the input is a good idea. It looks like you're really trying to do two things: first, you want to determine what the user's input was, and then you want to act on that input.

    So, first, read the user-inputted line. If your typical expected input is something like (bob AND casey) OR josh then it should be pretty simple to implement a regex-based grammar on the backend, though as juharr says, it's more complicated for you that way. But, assuming user input is AND/OR and grouped by parenthesis, you'll likely want to break it down like this:

    1. For each unit of filtering - each parenthetical - you want to hold 2 pieces of information: the items being filtered on (bob, casey) and the operator (AND)
    2. These units are order sensitive; filtering on (bob AND casey) OR josh is different from bob AND (casey OR josh). So you also want a higher level representation of the order to search in. For (bob AND casey) OR josh, you'll be determining validity of the search based on {result of bob AND casey} OR {result of josh}.

    These are probably objects. :)

    When you have your user input in a standardized form, you will want to sanity check it, and inform the user if you found something you can't parse (an unclosed parentheses, etc).

    After informing the user, then and only then should you perform the actual file searching. I would suggest treating each unit of search (item 2 above) as its own "search" and using a switch statement for the search operators, e.g.

        switch (operator) {
            case operator.AND:
                inString = fileLine.Contains(itemOne) && fileLine.Contains(itemTwo)
            case operator.OR:
                inString = fileLine.Contains(itemOne) || fileLine.Contains(itemTwo)
         }
    

    Additionally, you will want to handle cases where you are comparing bool AND string.Contains and bool OR string.Contains.

    If you have something like bob AND josh OR casey you could do a one-level linear comparison where you simply chopped them up and passed them through the comparison one at a time (bob and josh returns a bool, then pass that bool to a comparison for a bool OR string.Contains operation)

    This structure also means you're less likely to get spaghetti code if your scope changes (meaning, you won't have an ever-increasing series of if statements), and you can handle unpredictable input and notify the user if something is wrong.