2

Often I find myself writing if statements like this...

if (name.equals("fred") || name.equals("bob") || name.equals("jack")){

}

or with primitives...

if (numb==4 || numb==45 || numb=91){

}

Is there a more concise way to do this?

edit: My question is indeed a duplicate. To anyone reading this, I recommmend having a look at the other threads too - they have some good stuff.

WoodenKitty
  • 6,521
  • 8
  • 53
  • 73
  • 1
    `switch`......? [The switch Statement](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html) – MadProgrammer Jan 15 '15 at 01:16
  • what is gonna happen if you have && instead of ||? – Kick Buttowski Jan 15 '15 at 01:18
  • 2
    @KickButtowski That doesn't make sense. A value cannot be equal to multiple primitives, right? – k_g Jan 15 '15 at 01:19
  • @k_g I did not talk about this specific situation, yet I asked in general? – Kick Buttowski Jan 15 '15 at 01:20
  • Another duplicate here: http://stackoverflow.com/questions/20952325/a-cleaner-if-statement/20952404#20952404 – Kevin Workman Jan 15 '15 at 01:30
  • I don't think this should be a duplicate. yes, regarding strings, it is, but the original poster also asked for other objects and primitives. This isn't a String exclusive post. – EDToaster Jan 15 '15 at 01:30
  • If you have a moderately large number of strings, consider using a `Set`. Besides efficiency (since it will look for a hash instead of comparing with every string), it makes it easier to encapsulate the place where your `Set` is created, and possibly put the set data in a place that's more logical in your program. Also makes it easier to customize the data in the `Set` (e.g. if you're looking for a set of user commands, but then want to support users who speak a different language). – ajb Jan 15 '15 at 01:46

3 Answers3

3

You could use switch and case statements:

for example instead of:

if (numb==4 || numb==45 || numb=91){
    //do stuff
}

you could do:

switch(numb){
    case 4:
    case 45:
    case 91:
        //do something regarding the numbers 4 45 and 91
        break;
    default:
        break;
}

If you are big on readability (since it is what this question is concerned about), you could also create line breaks between the boolean checks:

if(name.veryLongMethodHere("fred") ||
   name.veryLongMethodHere("bob") ||
   name.veryLongMethodHere("food")){
    //do more stuff   
}

this is a very good way to organize code if you have very long and complicated methods.

Even better yet, using regex for String objects: if(name.matches("bob|food|fred"){ //do something }

EDToaster
  • 3,160
  • 3
  • 16
  • 25
2

You could...

Use a switch statement...

switch (name) {
    case "fred":
    case "bob":
    case "jack":
        break;
}

And

switch (numb) {
    case 4:
    case 45:
    case 91:
        break;
}

You could...

Use a Collection of some type...

if (Arrays.asList("fred", "bob", "jack").contains("fred")) {
    // Have match
}

Which is useful if you have dynamic values...

You could also use something like...

if (Arrays.asList("fred", "bob", "jack").containsAll(Arrays.asList("fred", "bob", "jack"))) {
    // Have match
}

If you need to to use && instead of ||...

You could also substitute the long hand String list with a predefined array or List, which could allow you to write a single method...

public <T> boolean matchOR(List<T> want, T have) {

    return want.contains(have);

}

public <T> boolean matchAnd(List<T> want, List<T> have) {

    return want.containsAll(have);

}

Which would allow you to write something like...

if (matchOr(Arrays.asList("fred", "bob", "jack"), name)) {
    //...
}

As an idea...

At some point, context will need to be considered, for example, the above examples allow you to provide a flexible series of conditions, but you might not want to do this and will have to write your if statements as you have been

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

The real answer is that you shouldn't always go for more "concise". Go for what's most readable, unless you're experiencing some kind of efficiency problem.

That being said, I'll throw in yet another way to do this:

    if(Arrays.asList("fred", "bob", "jack").contains(name)){

    }
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107