Checking a variable for multiple values?

I remember how a question was asked about the same problem where things like:

if( x==null || x==" " || x==" " || x=="\n")... 

They end up becoming long and ugly lines, but the answer was small, and I can't remember what it was.

I am in the middle of a MySQL tutorial, and the way to solve the problem in SQL is to use the keyword "IN".

 WHERE value IN (1 , 22, 35)... 

So, I was wondering if this is considered ineffective or bad practice:

 object[] BadVals = {null, " ", " ", "\n"}; if(Array.IndexOf(BadVals, x) != -1)... 
+4
source share
3 answers

It, of course, is ineffective in theory as a direct if test, but it is a red herring. The real question is: are you interested?

There are two sides to this question.

  • So what if it is slower? If you do not use this in a loop bound to a processor that runs a million times, the difference is purely theoretical. Use everything that makes for more mistakes that are nice to read and maintain.
  • So what if it's uglier? Are you going to write this many times? Of course not - if you intend to use it many times, put it in a well-named method and never think about it again.

As for the LINQ approach, it's a little shorter and a bit readable than yours:

 if((new[] { null, " ", " ", "\n" }).Contains(x)) ... 

You might want to write another extension method that allows you to call this with the translation of the operand positions, for example.

 if(x.In(new[] { null, " ", " ", "\n" })) ... 

Verdict:. I will go over with LINQ for more than three or so values ​​to check if there is no other more obvious way to check these values ​​(for example, in this case IsNullOrWhiteSpace terribly close) and there are no obvious performance implications. Otherwise, if checked and executed.

+6
source

Not necessarily bad practice, but there are other ways. Several ways:

  • For empty / empty lines

     String.IsNullOrEmpty(s.Trim()); 
  • IsNullOrWhiteSpace in 4.0:

      String.IsNullOrWhitespace(s.Trim()); 
  • for a statement of type SQL IN :

     if((new[] {" ", " ", "\n"}).Contains(s)) { } 
+3
source

These style issues are rarely absolute and depend heavily on the particular problem being addressed, as well as organizational considerations and personal preferences. This suggests that one well-known reference (Code Complete by Steve McDonnell) advises simplifying complex tests by using logical function calls as a means of reducing complexity in if statements. For example, you can replace the above if statement with the following:

 if (IsStringFoo(s)) {...} 

Elsewhere defined as

 // Returns true if and only if the input string is a Foo bool IsStringFoo(string s) { return s == "" || s == " " || s == "\n"; // or use the alternate array syntax here instead if you prefer } 

This tells the consumer not only logical mechanics, but also the potential business rationale for this mechanics, which probably improves readability.

Using the proposed in syntax can lead to similar readability problems - in particular, it is not yet clear why these specific tests are performed. The latter method can be potentially more difficult to understand, because "or" binds a logical logical operation in such a way that Array.IndexOf is not necessary.

+1
source

Source: https://habr.com/ru/post/1391355/


All Articles