Skip to content

Instantly share code, notes, and snippets.

@getify getify/1.js
Last active Apr 11, 2019

Embed
What would you like to do?
Nested ternary: good vs bad
var data = x ? y ? z ? z : y : x : 0;
// I call this the "if..if" pattern, and it's BAD. It means:
var data;
if (x) {
if (y) {
if (z) {
data = z;
}
else {
data = y;
}
}
else data = x;
}
else {
data = 0;
}
var data = x ? x : y ? y : z ? z : 0;
// can be formatted as:
var data =
x ? x :
y ? y :
z ? z :
0;
// I call this the "if..else if" pattern, and it's much BETTER. It means:
var data;
if (x) {
data = x;
}
else if (y) {
data = y;
}
else if (z) {
data = z;
}
else {
data = 0;
}
@amerllica

This comment has been minimized.

Copy link

amerllica commented Mar 23, 2019

Both of them are the root of bad readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.