0
if((x == 5) || (x == 2)) {  
    [huge block of code that happens]  
    if(x == 5)  
        five();  
    if(x == 2)  
        two();  
}

So I'm checking for either 5 or 2. And there is a huge block of code that happens after either 5 or 2. The problem is that then I want to do different things depending on whether it is 5 or 2. I didn't want to have separate conditionals for 5 or 2 for the huge block of code (duplicating it would be unwieldy). I also didn't like the way I did it above because x is actually really long.

Is there a way to say something like:

if((x == 5) || (x == 2)) {  
    [huge block of code that happens]  
    if(first conditional was true)  
        five();  
    if(second conditional was true)  
        two();  
}  

I can always do it the way I did above. Just curious if such an option exists.

  • 6
    I'd concern myself far more with [huge block of code that happens]. My first thought is "refactoring, anyone?" – duffymo Feb 23 '12 at 23:52
  • It worries me that so many heavy weight answering this question has not concern about the number of if statements used in this piece of code. – Shahzeb Feb 24 '12 at 00:06
  • i'm not an experienced coder. is this a serious problem (the number of conditionals)? – Anthony Newman Feb 24 '12 at 00:12
  • Yes absoulety check out this and I suggest subscribe to their new letter.http://www.antiifworkshop.com/ also check out this question http://stackoverflow.com/questions/1298672/avoiding-multiple-if-statements-in-java and this one http://stackoverflow.com/questions/3806822/java-using-polymorphism-to-avoid-if-statements . It is not possibly to write code entirely without using if or conditional check but good OO can surely reduce it to minimum. – Shahzeb Feb 24 '12 at 00:24

4 Answers4

3

If the conditionals are big, ugly, and much less nice than x == 5, then just store the results in a boolean:

boolean xWasFive = x == 5;
boolean xWasTwo = !xWasFive && x == 2;
if (xWasFive || xWasTwo) {
  ...
  if (xWasFive) doA;
  else if (xWasTwo) doB;
}
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
2

One way I can think of is basically to "alias" the longer boolean expressions in the if condition:

boolean expr1, expr2;

if (expr1 = (x == 5) | expr2 = (x == 2)) {  
    // huge block of code that happens
    if (expr1) five();
    if (expr2) two();
}

I used non short-circuiting operator to ensure expr2 gets assigned.

calebds
  • 25,670
  • 9
  • 46
  • 74
0

Only thing I can think of would be to set a flag for both options. Sort of like this:

boolean wasFive = x == 5;
boolean wasTwo = x == 2;

if(wasFive || wasTwo) {  
   [huge block of code that happens]  
   if(wasFive)  
       five();  
   if(wasTwo)  
       two();  
 } 
sauce
  • 592
  • 4
  • 9
  • 25
0

Maybe something like this:

final boolean firstCondition = (x == 5);
final boolean secondCondition = (x == 2);

if (firstCondition || secondCondition) {
    // code
    if(firstCondition) {
        // code 
    }
    else if (secondCondition) {
        // code
    }
}
Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118