Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A cleaner if statement with multiple comparisons [closed]

The following statement just looks very messy when you have a lot of terms:

if(a.equals("x") || a.equals("y") || a.equals("z") || Any number of terms...... )     //Do something 

Is there a cleaner way of performing the same action, I would like my code to be as readable as possible.

NOTE: x, y and z are just placeholders for any string of any length. There could be 20 string terms here of variable length in if condition each being OR'd together

like image 226
Allan Macmillan Avatar asked Jan 06 '14 14:01

Allan Macmillan


People also ask

How do you check multiple conditions in one if statement?

Here we'll study how can we check multiple conditions in a single if statement. This can be done by using 'and' or 'or' or BOTH in a single statement. and comparison = for this to work normally both conditions provided with should be true. If the first condition falls false, the compiler doesn't check the second one.

How do you write an if statement with multiple conditions in Java?

When using multiple conditions, we use the logical AND && and logical OR || operators. Note: Logical AND && returns true if both statements are true. Logical OR || returns true if any one of the statements is true.

How many conditions can an if statement have?

Technically only 1 condition is evaluated inside an if , what is ascually happening is 1 condition gets evaluated and its result is evaluated with the next and so on...

What does the if statement do?

The IF statement is a decision-making statement that guides a program to make decisions based on specified criteria. The IF statement executes one set of code if a specified condition is met (TRUE) or another set of code evaluates to FALSE.


2 Answers

What do you think looks "unclean" about it?

If you have a bunch of complicated boolean logic, you might separate the different parts of it into individual boolean variables and refer to them in the if statement.

Or you could create a function that takes your 'a' variable and returns a boolean. You'd just be hiding your logic in the method, but it would clean up your if statement.

like image 103
Kevin Workman Avatar answered Sep 25 '22 13:09

Kevin Workman


Set<String> stuff = new HashSet<String>(); stuff.add("x"); stuff.add("y"); stuff.add("z"); if(stuff.contains(a)) {     //stuff } 

If this is a tight loop you can use a static Set.

static Set<String> stuff; static {     stuff = new HashSet<String>();     stuff.add("x");     stuff.add("y");     stuff.add("z"); }  //Somewhere else in the cosmos  if(stuff.contains(a)) {     //stuff } 

And if you want to be extra sure nothing is getting modified while you're not looking.

Set<String> test = Collections.unmodifiableSet(new HashSet<String>() {         {             add("x");             add("y");             add("z");         }     }); 

If you just want to get some logic in there for a handful of hard coded conditions then one of the switch or if statement with newlines solutions might be better. But if you have a lot of conditions then it might be good to separate your configuration from logic.

like image 25
HahaHortness Avatar answered Sep 24 '22 13:09

HahaHortness