Reputation: 47
I have the below piece of code in Java. I need to execute all of the if statements.Is there a better approach to code this.In each of the statement, I would then make a database call.
if (!keyAccntId.equalsIgnoreCase("-1") && !(segmentId.equalsIgnoreCase("-1")) && !(regionId.equalsIgnoreCase("-1"))) {
templateOrder.add("1");
}
if (!keyAccntId.equalsIgnoreCase("-1") && (segmentId.equalsIgnoreCase("-1")) && !(regionId.equalsIgnoreCase("-1"))) {
templateOrder.add("2");
}
if (!keyAccntId.equalsIgnoreCase("-1") && (segmentId.equalsIgnoreCase("-1")) && (regionId.equalsIgnoreCase("-1"))) {
templateOrder.add("3");
}
if (keyAccntId.equalsIgnoreCase("-1") && !(segmentId.equalsIgnoreCase("-1")) && !(regionId.equalsIgnoreCase("-1"))) {
templateOrder.add("4");
}
if (keyAccntId.equalsIgnoreCase("-1") && (segmentId.equalsIgnoreCase("-1")) && !(regionId.equalsIgnoreCase("-1"))) {
templateOrder.add("5");
}
if (keyAccntId.equalsIgnoreCase("-1") && (segmentId.equalsIgnoreCase("-1")) && (regionId.equalsIgnoreCase("-1"))) {
templateOrder.add("6");
}
Upvotes: 4
Views: 281
Reputation: 108370
Only six of the possible combinations result in a call to add
method.
We note that the add
method only gets called once in the OP code, because the conditions in the if statements are mutually exclusive... if one of those evaluates as TRUE, the others will return FALSE.
To go old school with a logic truth table:
keyAccntId.equalsIgnoreCase("-1") | F | T |
segmentId.equalsIgnoreCase("-1") | F | T | F | T |
regionId.equalsIgnoreCase("-1") | F | T | F | T | F | T | F | T |
----------------------------------+---+---+---+---+---+---+---+---+
templateOrder.add("1"); | y | - | - | - | - | - | - | - |
templateOrder.add("2"); | - | - | y | - | - | - | - | - |
templateOrder.add("3"); | - | - | - | y | - | - | - | - |
templateOrder.add("4"); | - | - | - | - | y | - | - | - |
templateOrder.add("5"); | - | - | - | - | - | - | y | - |
templateOrder.add("6"); | - | - | - | - | - | - | - | y |
----------------------------------+---+---+---+---+---+---+---+---+
If the resulting values were "lined up" with the conditions better, then you could reduce the number of times the comparisons were done.
int n = 0;
if (keyAccntId.equalsIgnoreCase("-1") {
n += 4;
}
if (segmentId.equalsIgnoreCase("-1") {
n += 2;
}
if (regionId.equalsIgnoreCase("-1") {
n += 1;
}
templateOrder.add(String.valueOf(n));
NOTE: the code above is NOT equivalent to the OP code. To make it perform the equivalent, we'd need to add a conditional test, so that the add
method is not called when when n is 1 or 5, and to convert the value of n
to the specified string argument value:
n arg
--- ----
0 "1"
1 -
2 "2"
3 "3"
4 "4"
5 -
6 "5"
7 "6"
The logic table would look something like this:
keyAccntId.equalsIgnoreCase("-1") | F | T |
segmentId.equalsIgnoreCase("-1") | F | T | F | T |
regionId.equalsIgnoreCase("-1") | F | T | F | T | F | T | F | T |
----------------------------------+---+---+---+---+---+---+---+---+
n += 4; | - | - | - | - | y | y | y | y |
n += 2; | - | - | y | y | - | - | y | y |
n += 1; | - | y | - | y | - | y | - | y |
templateOrder.add(arg); | y | - | y | y | y | - | y | y |
----------------------------------+---+---+---+---+---+---+---+---+
Upvotes: 0
Reputation: 446
No "If", No "Switch". This 3 lines would cover all your cases:
int val = keyAccntId.equals("-1")? 3:0;
val += ((segmentId.equals("-1")? 1:0) + (regionId.equals("-1")? 1:0);
templateOrder.add(val+1+"");
.
You may want to encapsulated the code, so it would end up being something like this:
public int getTemplateOrder(int keyAccntId, int segmentId, int regionId){
int val = keyAccntId.equals("-1")? 3:0;
return val + (segmentId.equals("-1")? 1:0) + (regionId.equals("-1")? 1:0) + 1;
}
//Usage
templateOrder.add(getTemplateOrder(keyAccntId, segmentId, regionId)+"");
.
For readability these are those 3 lines:
int acc = keyAccntId.equals("-1")? 3:0;
int seg = segmentId.equals("-1")? 1:0;
int reg = regionId.equals("-1")? 1:0;
int val = acc;
//Construct the value currently being used on each If statement.
val += seg + reg;
//You've got the value, so instead of If/switch, just set it.
templateOrder.add(val+1+"");
I hope it's not too obscure.
Regards.
Upvotes: 2
Reputation: 533442
A shorter version of @njzk2's answer using Java 7.
public static final int KEY = 0b001, SEGMENT = 0b010, REGION = 0b100;
int comb = (keyAccntId.equals("-1") ? KEY : 0) +
(segmentId.equals("-1") ? SEGMENT : 0) +
(regionId.equals("-1") ? REGION : 0);
switch(comb) {
case 0: /* none are true. */ break;
case REGION: /* only regionId is -1 */ break;
// more combinations.
case KEY + SEGMENT + REGION: /* all are -1 */ break;
}
Upvotes: 7
Reputation: 39386
You could create a bitmask and switch on it:
public static final int KEY = 1;
public static final int SEGMENT = 2;
public static final int REGION = 4;
int value = 0;
if (keyAccntId.equalsIgnoreCase("-1")) {
value += KEY;
}
if (segmentId.equalsIgnoreCase("-1")) {
value += SEGMENT;
}
if (regionId.equalsIgnoreCase("-1")) {
value += REGION;
}
That gives you 8 possible values, which you can switch on:
switch(value) {
case 0:
// All false
break;
case KEY:
// only keyAccntId is true
break;
case REGION:
// only segmentId is true
break;
case KEY + REGION:
// segmentId and keyAccntId are true
break;
// So on
}
Added constants to improve readability
Upvotes: 16
Reputation: 834
To make things easier visually you could do something like this...
boolean keyTF = keyAccntId.equalsIgnoreCase("-1"),
segmentTF = segmentId.equalsIgnoreCase("-1"),
regionTF = regionId.equalsIgnoreCase("-1");
if (!keyTF && !segmentTF && !regionTF ) {
templateOrder.add("1");
}
if (!keyTF && segmentTF && !regionTF ) {
templateOrder.add("2");
}
if (!keyTF && segmentTF && regionTF ) {
templateOrder.add("3");
}
if (keyTF && !segmentTF && !regionTF ) {
templateOrder.add("4");
}
if (keyTF && segmentTF && !regionTF ) {
templateOrder.add("5");
}
if (keyTF && segmentTF && regionTF ) {
templateOrder.add("6");
}
The problem though is that you're dealing with a logical truth table, and you simply have to step through all of the possibilities. Since you're working with 3 separate variables there is no way to compress the code any further
FFF
FTF
FTT
TFF
TTF
TTT
you're actually missing the TFT case :)
Upvotes: 0