Dave
Dave

Reputation: 474

Using a bool for multiple conditions

In effort to make my code look more readable I am trying reduce the number times the conditions are written out for an if statement. The approach so far:

   bool op = token=="+"||token=="-"||token=="*"||token=="/"|| 
          token=="&"||token=="|"||token=="<"||token==">"|| 
          token=="="; 
    ...
    if(op==0){...}

The issue is that token is constantly changing! Is there anyway to work around this?

Upvotes: 1

Views: 53

Answers (1)

Nikem
Nikem

Reputation: 5784

Define a function with a meaningful name:

private boolean myBusinessCondition(String token){
  token=="+"||token=="-"||token=="*"||token=="/"|| 
      token=="&"||token=="|"||token=="<"||token==">"|| 
      token=="="
}

And use it whenever you like:

if(myBusinessCondition(tokenValue)){...}

Upvotes: 1

Related Questions