6yhmua
Last Updated: February 25, 2016
·
3.827K
· deleteman
04b056e843c7f0e4c7e97e36174e43b4

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.

Say Thanks
Respond

7 Responses
Add your response

8695
D42a7264714dee5006b9c99d2567a320

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 ·
8699
04b056e843c7f0e4c7e97e36174e43b4

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 ·
8761

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 ·
8893
Img 1931

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

over 1 year ago ·
8894
04b056e843c7f0e4c7e97e36174e43b4

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

over 1 year ago ·
9176
22e767367fe9c51fc5d22af7a631c424

'cause fuck readability, that's why

over 1 year ago ·
9177
04b056e843c7f0e4c7e97e36174e43b4

Indeed :)

over 1 year ago ·