re: 5 Programming Patterns I Like VIEW POST

FULL DISCUSSION
 

I think nesting ternaries makes things more difficult to change later without scratching your head. The linked article says it simplifies the conventional if

const withIf = ({
  conditionA, conditionB
}) => {
  if (!conditionA) return valueC;
  if (conditionB) {
    return valueA;
  }
  return valueB;
};

but it doesn't go all the way:

const withIf = ({
  conditionA, conditionB
}) => {
  if (!conditionA) return valueC;
  if (!conditionB) return valueB;
  return valueA;
};

At this point the if and the ternary are nearly identical, with the exception that the if statement blocks can contain anything they like without breaking the layout. Imagine if instead of returning a simple value they were all returning a long string or a calculation? I mean, you can say that these sorts of things should be factored into their own functions, but that's true of both techniques.

 

There is a very unfortunate drawback of using a function for "if" or ternary... the conditions are always evaluated and that often means dereferencing null or undefined...

code of conduct - report abuse