Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to eliminate if-else when doing different things according to different combinations of boolean value?

for example, suppose I need to do different things according to combinations of boolean values: cond_0,cond_1 and cond_2 :

cond_0 cond_1 cond_2
false  false  false  a();
false  false  true   b();
.
.
.
true   true   true   h();

it looks as if mapping bit numbers to functions:

000:a()
001:b()
.
.
.
111:h()

while the general rule looks like very simple, I don't know how to write it without if-else, and the current form looks like that:

var f=function(cond_0,cond_1,cond_2){
  if(!cond_0 && !cond_1 && !cond_2){
    a();
  }else if( cond_0 && !cond_1 && !cond_2)){
    b();
  }else if(!cond_0 &&  cond_1 && !cond_2)){
    c();
  }else if( cond_0 &&  cond_1 && !cond_2)){
    d();
  }else if(!cond_0 && !cond_1 &&  cond_2)){
    e();
  }else if( cond_0 && !cond_1 &&  cond_2)){
    f();
  }else if(!cond_0 &&  cond_1 &&  cond_2)){
  g();
  }else if( cond_0 &&  cond_1 &&  cond_2)){
    h();
  }
}

which is very long and hard to read. And when a new boolean condition cond_3 is added, it is horrible to modify the code:

if(!cond_0 && !cond_1 && !cond_2 && !cond_3){
    a();
  }else if( cond_0 && !cond_1 && !cond_2 !cond_3)){
    b();
  }
  .
  .
  .

Is there any way to eliminate the if else, so that cond_0 , cond_1 and cond_2 can just appear once only inside the function, and also easy to add new function when cond_3 is added? I want something like:

var f=function(cond_0,cond_1,cond_2){
  var magic=(000:a,001:b,010:c...);
  magic(cond_0,cond_1,cond_2)();
}
like image 339
ocomfd Avatar asked Feb 15 '18 03:02

ocomfd


People also ask

How do you write a boolean condition in an if statement?

An if statement checks a boolean value and only executes a block of code if that value is true . To write an if statement, write the keyword if , then inside parentheses () insert a boolean value, and then in curly brackets {} write the code that should only execute when that value is true .

What will happen if the first condition fails in an Boolean expression with and operator?

In most (but not all) modern languages, there is a feature called "short circuit boolean evaluation". This means that if the first part of an && condition is false, then the second part is not evaluated at all.

How many conditions can you have in an if statement?

Simple syntax Please note that the IFS function allows you to test up to 127 different conditions. However, we don't recommend nesting too many conditions with IF or IFS statements. This is because multiple conditions need to be entered in the correct order, and can be very difficult to build, test and update.

How do you check boolean conditions in Python?

We can evaluate values and variables using the Python bool() function. This method is used to return or convert a value to a Boolean value i.e., True or False, using the standard truth testing procedure.


1 Answers

See this implementation. Here all the three parameters (boolean expected) are multiplied by their place values to form the number and then padded with '0' to the desired length. This value is searched for in the magic object and the function if defined is called.

var a = function() {
  console.log('a() called');
};
var b = function() {
  console.log('b() called');
};
var c = function() {
  console.log('c() called');
};

var f = function(cond_0, cond_1, cond_2) {
  var cond = ((cond_0 * 100) + (cond_1 * 10) + (cond_2)).toString().padStart(3, '0');
  var magic = {
    '000': a,
    '001': b,
    '010': c
  };
  if (typeof magic[cond] === 'function') {
    magic[cond]();
  } else {
    console.log('No function defined for this cond');
  }
}

f(false, false, false);
f(false, false, true);
f(false, true, false);
f(true, false, false);
like image 116
Vivek Avatar answered Nov 15 '22 03:11

Vivek