3
top 2 comments
sorted by: hot top controversial new old
[-] itsnotlupus@lemmy.world 2 points 1 year ago* (last edited 1 year ago)

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 $$$("//*[contains(@class, 'post-')]//*[text()[contains(.,'fedilink')]]/../../..") 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.

[-] Marxine@lemmy.ml -1 points 1 year ago

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.

this post was submitted on 08 Jul 2023
3 points (80.0% liked)

Programmer Humor

19331 readers
187 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS