4 ms·
Prettier has made me change the code I write. It is now practical to write JS code as ternary expressions instead of if statements. Here's an example: const
by jeswin 9y ago
Prettier has made me change the code I write. It is now practical to write JS code as ternary expressions instead of if statements.
Here's an example:
const x = !(result instanceof Skip)
? result
: typeof obj === "object"
? traverseObject(Object.keys(obj))
: Array.isArray(obj)
? traverseArray(obj)
: new Skip("Not found.");
This was very hard to do prior to prettier; ternaries become very hard to read unless you carefully format them. If you're programming in this style, you'll find yourself reaching for IIFEs (immediately invoked fns) quite often, since JS doesn't yet have do expressions[1]. Here again, harder without prettier.
const x = items.length
? (() => {
const result = parse(deep(schema, params))
return !(result instanceof Skip) ? result : traverseArray(items.slice(1));
})()
: new Skip("Not found.");
[1] https://babeljs.io/docs/plugins/transform-do-expressions/ https://babeljs.io/docs/plugins/transform-do-expressions/
+ Minor edits for clarity
- morrbo 9y agoBoth of these things are way too hard to read and whilst they might be pretty you're massively screwing over whoever has to maintain your code lol
- saranshk 9y agoAgreed. Formatted code does not allow you to write stuff that is difficult to understand/reason about
- jeswin 9y agoThe same code written with if conditions will have very similar text, will be slightly longer, have a possibility of 'return' statements anywhere in the text, and more importantly - might have embedded mutations.
- GordonS 9y ago... and will be much easier to instantly grok for almost everyone
- jeswin 9y agoI won't win this argument, and ymmv. :) But personally, every identifier being a 'const' instead of a 'let' helps me reason about it much better, especially when the code is purely algorithmic. An alternative might be to use something like Facebook's Reason, where such code can be concise and idiomatic.
- VMG 9y agothere is a tradeoff between ease of comprehension for everybody and syntactic simplicity (that is easy to comprehend for those familiar)
- serpix 9y agoAnd full of missing return statements and corner cases. if it only were possible to put an expression anywhere you want :(
- z3t4 9y agoThere's nothing wrong with if statements. If you can't even understand the code yourself it's a sign that it needs to be rewritten. function x(result, Skip, obj, traverseObject, treverseArray) { if(! result instanceof Skip) return result; if(typeof obj === "object") return traverseObject(Object.keys(obj)); if(Array.isArray(obj)) return traverseArray(obj); return new Skip("Not found."); }
- benoits 9y agoAgreed. I don't see what is so evil about early returns, they seem very clear to follow to me. Especially with proper IDE syntax highlighting.
- ramblerman 9y agoThat is terrible... There is no performance gain, it's not particularly clever. Even if it's formatted better, I doubt this is easier to read than a few if statements for most people.