Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alternative approaches to multiple if else conditions [closed]

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?

like image 641
rubyist Avatar asked Sep 02 '20 15:09

rubyist


People also ask

Which of the following is an alternative of Multi Level else if statements?

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.


4 Answers

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");
like image 140
epascarello Avatar answered Sep 21 '22 05:09

epascarello


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.

like image 37
M Z Avatar answered Sep 21 '22 05:09

M Z


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' ifs :)

like image 24
sp00m Avatar answered Sep 24 '22 05:09

sp00m


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.
like image 45
HoldOffHunger Avatar answered Sep 24 '22 05:09

HoldOffHunger