-1

I have a situation where I have to exclude an ever growing list of conditions in an if statement that started out rather simple

I've thought of looping an list/array and I use RegEx when possible.

I would just like to know what others have done to try and simplify these situations.

here's a quick example:

if (!DataField.Contains(strText) & (strOtherText.IndexOf(strFindThis) < 0) || Regex.isMatch(strWithLetters, @"[A-Z]"))
{
    //Do things;
}
else
{
    //Do Other Things;
}

Still learning the RegEx but I'm working on it

So far it's been strings of several words or simply certain characters.

For example:

  • "#, &, ^, @, and more.."
  • "Form 9999-99 invalid client name"
  • Can't start with "https" or "http"
  • In some case if it contains a string like "Error Code" then it would be saved in an alternate file instead of simply being ignored for processing

I have 23 of these so far

mreinsmith
  • 154
  • 3
  • 14
  • 2
    That's not a complicated `if` condition. – Robert Harvey May 05 '16 at 18:09
  • 1
    Is that single `&` intentional in that condition? – rene May 05 '16 at 18:09
  • 1
    I should have mentioned that at this point i have 23 conditions to look for – mreinsmith May 05 '16 at 18:12
  • 1
    Can you state, in plain English, what your "if" statement is evaluating for (as a high-level summary)? It sounds like you might be able to re-think your approach. – Colin May 05 '16 at 18:13
  • Still learning C#, came from years of VB, i have to look at the Operators again for and-or etc. Its supposed to be AND – mreinsmith May 05 '16 at 18:14
  • 1
    you can use the command pattern, some examples can be found here: http://stackoverflow.com/questions/1199646/long-list-of-if-statements-in-java – Dn24Z May 05 '16 at 18:21
  • Hard to say without seeing all the conditions, but maybe you can have a switch statement, then break down into smaller if thens inside each case. – Baddack May 05 '16 at 18:22

3 Answers3

1

It really depends on your conditional expressions, but you could use something like:

List<Func<bool>> expressions = new List<Func<bool>>();
expressions.Add(()=>anyConditionalExpression());

if(expressions.All(x=>x.Invoke())){
  //do something;
}
else{
     //do something else;
}
wajiro_91
  • 170
  • 1
  • 9
  • I dont think that this is advisable for code maintenance and organization. Maybe break conditions into "groups" and have a method that checks each group would be a better answer. – Marcello Grechi Lins May 05 '16 at 18:29
  • How do you mean? That looks pretty clean and I could store the conditions in a file or database. So if there is even a better way that would be awesome because I have a feeling this will keep coming. – mreinsmith May 05 '16 at 18:32
  • Of course. Anyways I don't really believe that checking 23 conditional expressions in just a method is advisable. – wajiro_91 May 05 '16 at 18:32
  • So how should i be approaching it. I am parsing large text logs line by line to identify pertinent records. The process is old and the debug information extensive. For some reason they don't want to turn the level down. – mreinsmith May 05 '16 at 18:41
  • If u r parsing the text line by line u should be using an iterator. So add the expression to the list inside the iterator and after that check the expressions list. – wajiro_91 May 05 '16 at 18:48
  • Still working on it but yes that is how i want to go, Thanks – mreinsmith May 05 '16 at 20:24
1

I would create an interface an implement the various validation criteria you are required to support. Once completed, the implementing objects can be put into a collection which can be passed off to the validation method.

public interface IValidator
{
    bool IsValid(String currentLine);
}

The concrete classes would be implemented to support your rules: ContainsStringValidator and RegexMatcherValidator come to mind from you example above.

List<IValidator> rules = new ArrayList<IValidator>();
rules.Add(new ContainsStringValidator("StringToLookFor"));
rules.Add(new RegexMatcherValidator(@"[A-Z]"));

Now that the rules have been collected into a manageable unit you can write your code to parse the files.

public void CheckLine(String line, List<IValidator> rules)
{
    foreach (IValidator v in rules)
    {
        if (v.IsValid(line))
        {
           // it works
        }
        else
        {
           // do something when bad
        }
    }
}
Ken Brittain
  • 2,255
  • 17
  • 21
0

Yeah it's hard to give a specific answer without seeing your conditions, but in general:

Put each condition on a new line.

If you have groups of conditions that do the same thing but with different input, you could abstract them into a function that checks several conditions at once.

For example,

if(str1 != null && str1.Length > 5 && str2 != null && str2.Length > 5)
{...}

into

bool isNice(str)
{
   return str != null && str.Length > 5;
}

if(isNice(str1) && 
   isNice(str2))
{...}
Dmiters
  • 2,011
  • 3
  • 23
  • 31