Last Updated: February 25, 2016
·
5.943K
· deleteman

Turn an IF-ELSE statement into a one liner without using a ternary operator

We all know about the ternary operator, I'm not talking about it. I'm talking about dealing with something like this:

if(Array.isArray(mixedVariable)) {
   mixedVariable[idx] = value;
} else if(!NaN(mixedVariable)) {
   mixedVariable = value + 10;
} else {
   mixedVariable = value;
}

In that case, a ternary operator is not enough. But we can also turn it into

((mixedVariable[idx] = value) && Array.isArray(mixedVariable)) || ((mixedVariable = value+10) && !isNaN(mixedVariable)) || (mixedVariable = value)

Having the assignments surrounded by parenthesis is crucial, otherwise you'll end up having a boolean value on the first two cases.

Disclaimer
It might be helpful in some cases to simplify some statements and in some other cases it might be a very bad idea to write your code this way. Left to the developer's discretion.

7 Responses
Add your response

The one-liner example you provided is too cryptic and unreliable, I would prefer the first code block in this case. I like using logical operands for quick short conditionals but I rarely go beyond something like

someCondition && doActionIfTrue();
anotherCondition || doActionIfFalse();

I came up with the following rules of thumb in that area:

  • Never use assignments in conditionals (even in a regular if-block). They are easy to overlook, besides you have to enclose them in extra parentheses if you want to use them in a one-liner (looks a bit messy)
  • Don't use more than 1-2 actions in a logical one-liner (someCondition && doActionIfTrue();)
  • If you want an if-else one-liner substitute, it's better to use the ternary operator instead if && and ||, especially if you want to assign assign the result of the expression to a variable. E.g. x = condition ? method1() : method2() is shorter and also more robust than x = condition && method1() || method2(), because in the second case you won't get the intended result if condition is true and method1 returns false
  • Sometimes it's OK to nest ternary operators but not deeper than 2 levels (and definitely break it into several lines with nice formatting)
  • Finally, clarity is more important than brevity. Please don't write one-liners like in the original example.
over 1 year ago ·

Thanks for your comments, I appreciate your input and I do agree with you in all of those points, but I have come to use this in some cases, so I thought I'd share it here.

over 1 year ago ·

That's not a good example, while simple examples such as dpashkevich pointed out can improve readability example you provide doesn't. And on top of that it makes code harder to modify and refactor.

over 1 year ago ·

Never say never, re-assignment in conditionals is a fairly popular and general practice.

over 1 year ago ·

Thanks, finally someone said the magic words... "never say never", that's the whole point of this pro-tip..

over 1 year ago ·

'cause fuck readability, that's why

over 1 year ago ·

Indeed :)

over 1 year ago ·