I have a question of how to best order your code when using multiple if statements that uses the same object in their logic.
I am customizing column values in a table where depending on the columnId a different logic will be applied to extract the value for that specific column.
Given that I have three different columns namned "Column 1", "Column 2" and "Column 3" where both "Column 1" and "Column 3" use a customized object called obj in their logic whereas "Column 2" doesn't.
What is the best approach to order the if statements and where should I create my object? I've added three different cases, which one is best and why? Another better solotuion?
Thanks in advance!
// Case 1
public Object getColumnValue(String columndId) {
if (columndId.equals("Column 1") || columndId.equals("Column 3")) {
customObject obj = new customObject();
if (columndId.equals("Column 1")) {
// do something with the custom object
} else if (columndId.equals("Column 3") ||) {
// do something else with the custom object
}
} else if (columndId.equals("Column 2")) {
// do something without the custom object
} else {
return null;
}
}
// Case 2
public Object getColumnValue(String columndId) {
customObject obj = new customObject();
if (columndId.equals("Column 1") {
// do something with the custom object
} else if (columndId.equals("Column 2")) {
// // do something without the custom object
} else if (columndId.equals("Column 3")) {
/// do something with the custom object
} else {
return null;
}
}
// Case 3
public Object getColumnValue(String columndId) {
if (columndId.equals("Column 1") {
customObject obj = new customObject();
// do something with the custom object
} else if (columndId.equals("Column 2")) {
// // do something without the custom object
} else if (columndId.equals("Column 3")) {
customObject obj = new customObject();
/// do something with the custom object
} else {
return null;
}
}
Aucun commentaire:
Enregistrer un commentaire