I don't want to use ternaries
I don't want to use ternaries
One of my guilty pleasures is to rewrite trivial functions to be statements free.
Since I’d be too self-conscious to put those in a PR, I keep those mostly to myself.
For example, here’s an XPath wrapper:
const $$$ = (q,d=document,x=d.evaluate(q,d),a=[],n=x.iterateNext()) => n ? (a.push(n), $$$(q,d,x,a)) : a;Which you can use as $$$(“.//*[text()[contains(.,‘copy url to fediverse’)]]”) to get an array of matching nodes.
If I was paid to write this, it’d probably look like this instead:
function queryAllXPath(query, doc = document) { const array = []; const result = doc.evaluate(query, doc); let node= result.iterateNext(); while (node) { array.push(node); n = result.iterateNext(); } return array; }Seriously boring stuff.
Anyway, since var/let/const are statements, I have no choice but to use optional parameters instead, and since loops are statements as well, recursion saves the day.
Would my quality of life improve if the lambda body could be written as => if n then a.push(n), $$$(q,d,x,a) else a ? Obviously, yes.
For a long time I hated ternaries, partially due to my experience with them in PHP (Who thought left-associative ternaries was a good idea? seriously?).
OpenSCAD made me love them again. It’s purely functional so you’re encouraged to use nested ternaries.
Having some experience with both Python and JS/TS, I don’t have much preference about ternaries or expressions. Although I always break lines for ternary statements.
const testStuff = condition ? outcome(1) : outcome(2);Having everything on the same line ruins readability for me.
personally I prefer
const testStuff = condition ? outcome(1) : outcome(2);The if-else expression that Python has is quite different from (and significantly worse than) what people mean with if-else as an expression.
So, this is Python:
volume = 100 if user_is_deaf else 50These are two examples of if-else as an expression (Rust and Scala):
let volume = if user_is_deaf { 100 } else { 50 }; val volume = if (user_is_deaf) 100 else 50Crucially, these look essentially equivalent to normal if-else-statements in these languages.
and then you have lua
local result = condition and a or bI’m not complaining, although it gets a little confusing when one of the results is falsey. Which is a rarity since only false and nil are falsey in lua.