r/javascript • • Mar 27 '15

Airbnb JavaScript Style Guide - A mostly reasonable approach to JavaScript

https://github.com/airbnb/javascript/blob/master/README.md
315 Upvotes

158 comments sorted by

View all comments

Show parent comments

2

u/nschubach Mar 27 '15

I fall in the camp that nested ternaries are bad and should be refactored to make more sense to the casual reader so setting breakpoints on indented ternaries will never be in my code.

2

u/Asmor Mar 28 '15

I find use cases for ternaries exceedingly rare. I only use them in trivial cases, like:

var foo = (useA) ? a : b;

Even then, I'm a lot more likely to do:

var foo = b;
if (use A) {
    foo = a;
}

Pretty much only really use them when the case is trivial and there's an aesthetic benefit to keeping the whole assignment on one line (e.g. it's just one in a long list of things being assigned).

2

u/alamandrax Mar 28 '15

I've banned ternary usage except for the trivial case you've cited. The code is getting a lot more readable now.

(someCondition) ? executeApi() : variable = assignSomething;
var myBool = (testVarExistance) ? null : testVarExistance;

to illustrate some choice examples. Don't get me started on 3 levels of nested termaries.