Haifeng Zhang
Haifeng Zhang

Reputation: 31915

Simplify multiple if-else statements for unit testing

I am writing some java code to check multiple conditions by if-else. The code is working properly but it is hard to do unit test.

reads lines that contains keyword conditionOne, conditionTwo or other keywords. hasConditionOneEnabled and hasConditionTwoEnabled are boolean values.

My real code has more else if statements than the provide example. Can anyone help? Or give me some hint how to make the code shorter then I can write unit test easier? Thanks

boolean a = false;
boolean b = false;


if(line.contains("conditionOne")){
    if(hasConditionOneEnabled){
        a = true;
    }else{
        b = true;
    }
}else if (line.contains("conditionTwo")){
    if(hasConditionTwoEnabled){
        a = true;
    }else{
        b = true;
    }
}else{
    a = true;
    b = true;
}


if(a && b){
    // do something 1
}else if(!a && b){
    // do something 2
}else if(a && !b){
    // do something 3
}else{
    //both false, do nothing
}

Upvotes: 0

Views: 4385

Answers (4)

Srini
Srini

Reputation: 1646

First of all both a and b can never be false, so your last else statement is redundant.

Your entire set of conditional statements can be reduced to an if - else if - else block. You don't need variables a and b since you are using them to do something else anyway. Besides vague variables names like a and b hinder readability.

Let me first show you the code and I'll walk you through it subsequently.

boolean lineContainsCond1 = line.contains("conditionOne");
boolean lineContainsCond2 = line.contains("conditionTwo");

boolean lineContainsNeitherCondition = !lineContainsCond1  && !lineContainsCond2;       

boolean conditionsForSomething3 = (lineContainsCond1 && conditionOneEnabled) || (lineContainsCond2 && conditionTwoEnabled);

if(lineContainsNeitherCondition)
    //do something 1 (Note: this is the same something 1 from your code)
else if(conditionsForSomething3)
    //do something 3
else
    //do something 2

lineContainsNeitherCondition is essentially both a and b being true in your code.

conditionsForSomething3 tantamounts to a!b.

If both lineContainsNeitherCondition and conditionsForSomething3 are false, we can derive the following conclusions:

  • Given lineContainsNeitherCondition is false, either lineContainsCond1 is true or lineContainsCond2 is true
  • Case 1 : lineContainsCond1 is true: In this case, either conditionOneIsEnabled is true or conditionOneEnabled is false. If it were true, then conditionFOrSomething3 cannot be false, if it's false, then that leads to lineContainsCond && !conditionOneEnabled to be true which leads to b!a in the original code and thereby executes //something 2.

A similar argument can be made for Case 2 : lineContainsCond2 is true.

Upvotes: 0

phdfong - Kenneth Fong
phdfong - Kenneth Fong

Reputation: 880

// test it on different line String input and different int value returned...
int xxx(String line) {
    if(line.contains("conditionOne")){
        status = hasConditionOneEnabled?0:1;
    } else if (line.contains("conditionTwo")){
        status = hasConditionTwoEnabled?0:1;
    } else{
        status = -1;
    }

    return status;
}


// test it base on different status value..
switch (status) {
case 0: ...;
case 1: ...;
default: ...;
}

However, if your if-else pattern can be continuously repeat after some modification, you may just create different boolean funciton for it.

Upvotes: 0

Horacio
Horacio

Reputation: 124

a and b cannot be both false after the set of if-else statements. In the first two if's variable a will have the same value than the corresponding hasConditionXXEnabled and b will be set as the opposite. The default else will set both to true.

Consider the following code:

a = true;
b = true;

if(line.contains("conditionOne")){
    a = hasConditionOneEnabled;
    b = !a;
}
else if(line.contains("conditionTwo")){
    a = hasConditionTwoEnabled;
    b = !a;
}

if(a && b){        
    // do something 1
}
else if(b){
    // do something 2
}
else{
    // do something 3
}

Upvotes: 3

Durand Dsouza
Durand Dsouza

Reputation: 13

Why don't reduce the amount of if else statements in your code.

Try replacing the if else statements with private methods that return a boolean. Try to in cooperate the below methods or similar methods into your above code.

Having a look at mookito great for mocking and stubbing. If you have a big project with lots of Objects will save you hours maybe days.

private boolean doesLineContainCondition(String line, String  searchPhrase) {
 if(line.contains(searchPhrase) {
  return true;
 } else {
  return false;
 }
}


private boolean hasConditionBeenEnabled(boolean condition) {
 if(condition) { 
   a = true;
  }
 else {
  b= true;
 }
}

Upvotes: -1

Related Questions