Reputation: 23
anyone has ideas about making this block of if-else statements simpler other than purely using switch-case statements? Thanks for your time!
public void onButtonClick(int type, int state) {
if (type == 1) {
if (state == 1)
jsonDataToString(); // method 1
else if (state == 2)
filterRecordFile(); // method 2
else if (state == 3)
filterResourceFile(); // method 3
} else if (type == 2) {
if (state == 1)
jsonDataToString(); // method 1
else if (state == 2)
filterHasBackup(); // method 4
else if (state == 3)
filterNotBackup(); // method 5
} else if (type == 3) {
if (state == 1)
jsonDataToString(); // method 1
else if (state == 2)
filterOverSize(); // method 6
else if (state == 3)
filterDownSize(); // method 7
}
}
Upvotes: 2
Views: 155
Reputation: 23
thanks for your help. Here I found a great and detailed explanation: https://programmer.help/blogs/java-optimization-28-optimizing-if-else-writing.html
His blog gives 8 optimization schemes to optimize if-else writing. I leave it here for late-comers :-)
Upvotes: 0
Reputation: 2505
I would factor out the state
handling and convert it to switch
es, like:
public void onButtonClick(int type, int state) {
if (type == 1) {
handleTypeOne(state);
} else if (type == 2) {
handleTypeTwo(state);
}
}
private void handleTypeOne(int state) {
switch(state) {
case 1:
jsonDataToString(); // method 1
break;
case 2:
filterRecordFile(); // method 2
break;
case 3:
filterResourceFile(); // method 3
break;
default:
break;
}
}
private void handleTypeTwo(int state) {
switch(state) {
case 1:
jsonDataToString(); // method 1
break;
case 2:
filterHasBackup(); // method 4
break;
case 3:
filterNotBackup(); // method 5
break;
default:
break;
}
}
This allows you to choose more meaningful method names than I did. Besides (and unrelated to your question), I would recommend to introduce constants for the state
values in order to make the case
selections more comprehensible.
Upvotes: 1
Reputation: 338
If you just want to make your code easier to read, you could try to format it differently by combining both conditions. Although you have to write type == 1
multiple times, it reduces the amount of nested if statements, which makes the code more readable.
public void onButtonClick(int type, int state) {
if (type == 1 && state == 1) jsonDataToString();
if (type == 1 && state == 2) filterRecordFile();
if (type == 1 && state == 3) filterResourceFile();
if (type == 2 && state == 1) jsonDataToString();
if (type == 2 && state == 2) filterHasBackup();
if (type == 2 && state == 3) filterNotBackup();
}
Upvotes: 1
Reputation: 1362
You can use tabular functions for such handlers. For example:
class Main {
private static final Runnable[][] HANDLERS_TABLE = {
{ Main::jsonDataToString, Main::filterRecordFile, Main::filterResourceFile },
{ Main::jsonDataToString, Main::filterHasBackup, Main::filterNotBackup }
};
private static void jsonDataToString() {
System.out.println("Hello jsonDataToString!");
}
private static void filterRecordFile() {
System.out.println("Hello filterRecordFile!");
}
private static void filterResourceFile() {
System.out.println("Hello filterResourceFile!");
}
private static void filterHasBackup() {
System.out.println("Hello filterHasBackup!");
}
private static void filterNotBackup() {
System.out.println("Hello filterNotBackup!");
}
public static void onButtonClick(int type, int state) {
HANDLERS_TABLE[type][state].run();
}
public static void main(String[] args) {
onButtonClick(1, 1);
}
}
https://replit.com/join/yzjqwikvil-redneckz
Also, it makes sense to use enums for keys. For example:
enum HandlerType {
FIRST, SECOND
}
public static void onButtonClick(HandlerType type, int state) {
HANDLERS_TABLE[type.ordinal()][state].run();
}
Upvotes: 1