alternative approaches to multiple if else conditions
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");
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:
- Readable by people without programming skills.
- Readable, verifiable, and confirmable by computer parsers.
- JSON files can be stored outside of your code, and only loaded by the JS when necessary.
- By sectioning off data from code, people can work on the data without affecting code, and vice versa.
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.