Home > Blockchain >  Simply if statements es6
Simply if statements es6

Time:12-27

Is there any way to make these below if statements more simple -

if (data1 && data2 && data3) { return 'A'; }
if (!data1 && data2 && data3) { return 'B'; }
if (!data1 && !data2 && data3) { return 'C'; }

CodePudding user response:

You can nest them to combine common conditions.

if (data1) {
    if (data2 && data3) {
        return 'A';
    }
} else {
    if (data3) {
        if (data2) {
            return 'B';
        } else {
            return 'C';
        }
    }
}

Whether this is "simpler" is a matter of personal taste.

CodePudding user response:

I tend to prefer lookup tables rather than nested if/then else statements as it generally seems like data structures are simpler to maintain and extend than lines of actual logic in code. This table is extensible as far as you want to go.

With three booleans, there are 2^3 = 8 possible permutations. If you only want to account for the specific 3 permutations in your question, you can use a sparse lookup table and a little math to calculate a lookup index. Values other than the ones in the lookup table will return undefined from the lookup:

// Values to test for:
// true, true, true - 111
// false, true, true - 11
// false, false, true - 1
const table = {
    "111": 'A',
    "11": 'B',
    "1": 'C'
}

const data1 = false;
const data2 = true;
const data3 = true;
const lookupIndex = (!!data1 * 100)   (!!data2 * 10)   (!!data3);
console.log(table[lookupIndex]);

No if/else logic at all. Just a calculation and a table lookup.

Note the !! to coerce the values into booleans in case they aren't booleans and then the * operator will convert them to 0 or 1.

Also, note that the code savings here grows significantly if you start looking for all possible 8 permutations or if you then had more variables to add to the mix.

This converts to decimal, weighing each boolean as a power of 10 (data1 is 10^2, data2 is 10^1 and data3 is 10^0). It could convert to binary, weighing each boolean as a power of 2, but for this small number of items, decimal seemed easier.


Or, here's a version where you actually leave the boolean values in the table which I think I like better:

const table = {
    "true,true,true": 'A',
    "false,true,true": 'B',
    "false,false,true": 'C'
}

const data1 = "";
const data2 = "hello";
const data3 = true;
const lookupIndex = `${!!data1},${!!data2},${!!data3}`;
console.log(table[lookupIndex]);


If you wanted to account for all 8 possibilities and perhaps wanted to even expand to include other non-boolean values in the lookup table, you could use a full and extensible lookup table. This table is extensible to values other than just boolean values (strings or numbers can be used if you remove the !! boolean coercion:

const table = {
    true: {
        true: {
            true: 'A',
            false: 'true/true/false'
        },
        false: {
            true: 'true/false/true',
            false: 'true/false/false'
        }
    },
    false: {
        true: {
            true: 'B',
            false: 'false/true/false'
        },
        false: {
            true: 'C',
            false: 'true/false/false'
        }
    }
}

const data1 = false;
const data2 = true;
const data3 = true;

console.log(table[!!data1][!!data2][!!data3]);

  • Related