I have multiple conditions to be checked and executed like below.
if (date == current_date && source === "s3") {
table_name = "Table1";
} else if (date == current_date && source !== "s3") {
table_name = "Table2";
} else if (date !== current_date && source === "s3") {
table_name = "Table3";
} else if (date !== current_date && source !== "s3") {
table_name = "Table4";
}
I think using switch statement doesn't make sense here since we are not evaluating case statement expression against switch expression.
So is it okay to go with multiple if else statements or any better alternative approach?
Another alternative to using if statements is a dynamic dispatch. This involves selecting which polymorphic method to call based on an object's type. It could be used to branch conditionally, like this: Here, a different code path is taken depending on the type of object passed to the handleShape function.
Your code is 100% a good option. It is just a bit hard to read. You can pull out common code into variable to make it more readable
var isCurrent = date == current_date;
var isS3 = source === "s3";
if (isCurrent && isS3) {
table_name = "Table1";
} else if (isCurrent && !isS3) {
table_name = "Table2";
} else if (!isCurrent && isS3) {
table_name = "Table3";
} else {
table_name = "Table4";
}
Other option is to use ternary operators
var isCurrent = date == current_date;
var isS3 = source === "s3";
if (isCurrent) {
table_name = isS3 ? "Table1" : "Table2";
} else {
table_name = isS3 ? "Table3" : "Table4";
}
It could be one big ternary, but it is a bit unreadable
var isCurrent = date == current_date;
var isS3 = source === "s3";
table_name = isCurrent ?
(isS3 ? "Table1" : "Table2") :
(isS3 ? "Table3" : "Table4");
In this particular case, it might make for more logical understanding if we simplified it into a nested if statement:
if (date == current_date) {
if (source === "s3") {
table_name = "Table1";
} else {
table_name = "Table2";
}
} else {
if (source === "s3") {
table_name = "Table3";
} else {
table_name = "Table4";
}
}
This does do at most 2 logical comparisons and using program control instead, while achieving the same logical result, whereas yours will take up to... 8?
But at this point, it's mostly a nit-picking style issue, and the comments have a few good ideas. For example, if you were expecting this logic to grow, it would make a lot more sense to use a map/object to store this information.
And yes, a switch statement does not make much sense.
Here's my application for "the most over-engineered solution" contest:
const bools = [
date === current_date,
source === "s3",
// more?
];
const mask = bools.reduce((x, e) => x + +e, "");
// [ false, false ] --> 00
// [ true, false ] --> 10
// [ false, true ] --> 01
// [ true, true ] --> 11
switch (mask) {
case "00":
table_name = "Table4";
break;
case "01":
table_name = "Table3";
break;
case "10":
table_name = "Table2";
break;
case "11":
table_name = "Table1";
break;
default:
// noop
break;
}
Here could be another mask to switch
over if you prefer, numerical this time:
const mask = bools.reduce((x, e, i) => x + e * Math.pow(2, i), 0);
// [ false, false ] --> 0
// [ true, false ] --> 1
// [ false, true ] --> 2
// [ true, true ] --> 3
Otherwise, just use your good ol' if
s :)
How about storing the conditions in a JSON structure? Like this...
hash = {"Table1current":"Table1", "Table1notcurrent":"Table4", etc....};
Then to access the value you want, just do...
table = "Table1";
current = date == current_date ? 'current' : 'notcurrent';
table_name = hash[table + current];
That way, your conditions are represented as a JSON structure, which has more advantages:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With