1

I have the following:

var cond1 = (a>b) ? true: false;
var cond2 = (d<c) ? true: false;
var cond3 = (e==f) ? true: false;

var found = cond1 && cond2 && cond3;

I want to be able in an elegant way to substitute the && logical operator for another like || and do this programmatically, as in a sort of "variable" operator. Because I don't want to do something like:

var found1 = cond1 && cond2 && cond3;
var found2 = cond1 || cond2 || cond3;
var found3 = (cond1 && cond2) || cond3;

and then switch/if depending on what choice the user has selected from the interface, it's not elegant.

How can I do this ? (if possible)

Thank you in advance

3
  • I would start doing just what I need such as var cond1 = a > b; and finally || is as much operator as && is, substitute one for another definatelly does not make your code more elegant. Commented Jun 21, 2018 at 15:45
  • So you want a function like CheckConditions that takes operators as parameters and compares cond1,2,3? So your found variables could be found1 = CheckConditions('&&', '&&')? Commented Jun 21, 2018 at 15:47
  • the ternary operator is unnecessary. var cond1 = a > b will give exactly the same result as var cond1 = a > b ? true : false. the result of expression a>b is already true or false. Commented Jun 21, 2018 at 16:14

3 Answers 3

2

I would start with something like a method which can take a string (AND/OR) and an array of bool delegates and perform the action(s)

function operator(which, ...args)
{
    switch(which.toUpperCase())
    {
        case "AND": return args.every(a => a());
        case "OR": return args.some(a => a());
        default: throw "Invalid operator " + which;
    }
}

var a = 1;
var b = 2;
var c = 3;
var d = 4;
var e = 5;
var f = 5;

var cond1 = () => (a>b) ;
var cond2 = () => (d<c) ;
var cond3 = () => (e==f);

var found1 = operator("and",cond1,cond2,cond3);
var found2 = operator("or",cond1,cond2,cond3);
var found3 = operator("or", () => operator("and",cond1,cond2), cond3);
console.log(found1,found2,found3)

From there its easy enough to build up dynamically.

References

Sign up to request clarification or add additional context in comments.

3 Comments

@ManosKounelakis It wasnt at first, I missed the tag but have updated the answer since then
@Jamiec I am not sure I understand where the a => a() comes from in the operator function.I suppose it's the function that is run by the .some / .every ?
@MIrrorMirror it represents the individual functions you pass in to the function. You could just do some(a) but I thought that would be less clear than .some(a => a())
1

I think elegant way of writing this is code should be readable and easy to understand. Making a logic complex to achieve simple thing doesn't make sense in my opinion. Things which kept simple and clean is less bug prone than complex logic, it gives us the confidence on our code.

Here you can store && and || in variable and can pass to the function.

var cond1 = (2>3);
var cond2 = (5<9);
var cond3 = (5==7);

const compareConditions = (operator, operand1, operand2) => {
  switch(operator){
    case "&&": {
      return operand1 && operand2;
    }
    case "||":{
       return operand1 || operand2;
    }
    default:{
      return false;
    }
  }
}

console.log(compareConditions("||", compareConditions("&&" , cond1, cond2), cond3));

2 Comments

However this does not preserve the short-circuit behavior of the logical operators.
Yes, It doesn't.
0

Depending on complexity, I'd either go with something similar to what you already have, or if there are a lot of options, I might go with some sort of flags/bitmask solution.

There are some good examples of this in JavaScript here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.