this post was submitted on 08 Jul 2023
137 points (93.1% liked)

Programmer Humor

19706 readers
164 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
 
top 18 comments
sorted by: hot top controversial new old
[–] [email protected] 23 points 1 year ago* (last edited 1 year ago) (1 children)

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.

[–] [email protected] 3 points 1 year ago (2 children)

Reminds me when my I did all my homework using list comprehension, ternary operations, and lambda expressions because it was boring. Here's an example

def task03( matrix ):
    print((new_matrix := [[ (y if y != 0 else ( temp := sum([r[j] for r in matrix[0:i]] + x[j:]), zeros_sum := (zeros_sum if 'zeros_sum' in locals() else 0) + temp)[0] ) for j,y in enumerate(x)] for i,x in enumerate(matrix)], '\n'.join( [ ' '.join([str(new_matrix[o][p]) for p in range(0 if len(matrix[0]) <= 20 else len(matrix[0])-20, len(matrix[0]))]) for o in range(0 if len(matrix) <= 20 else len(matrix)-20, len(matrix))  ] ) + '\n' + str(zeros_sum) )[1] )

[–] occams_chainsaw 3 points 1 year ago

you monster

[–] [email protected] 9 points 1 year ago* (last edited 1 year ago) (1 children)

The whole idea of expressions is very nice, and I can't imagine using ternary expressions anywhere after learning Rust.

Also implicit returns ❤️

[–] msage 1 points 1 year ago (2 children)

I never got to like implicit anything.

Not even returns. Ever.

[–] [email protected] 1 points 1 year ago

Doing everything explicitly can get to be annoying, especially when it comes to what you had to do before without Vulkan's VK_EXT_shader_object.

It's clear that some stuff should be implicit - most types in programming languages, for example; needing to specify a struct type and then the struct itself can be annoying - and other stuff explicit, like low level operations.

Returns are something that usually fall into that "implicit" category. Why should I do let a = function(); return a; when I can just do function()? It's shorter, simpler, and I don't waste keystrokes.

[–] [email protected] 1 points 1 year ago

You get used to it pretty quickly. After a while you wonder how you ever lives without it. Explicit returns feel like ending an if with endif. The end of the conditional's scope is implied by the end of the block by } or whatever.

[–] [email protected] 7 points 1 year ago

I like using if expressions in kotlin, but secretly sometimes I miss ternaries

[–] [email protected] 5 points 1 year ago (2 children)

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.

[–] lee 3 points 1 year ago (1 children)

personally I prefer

const testStuff = condition
  ? outcome(1) 
  : outcome(2); 
[–] [email protected] 1 points 1 year ago

Also works fine and is better than inlining it all. I'm just more used to ending the lines with the symbols - instead of starting the next line with them like your example - because it's the same parttern I use for other stuff, like (curly) brackets.

[–] [email protected] 2 points 1 year ago

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 50

These 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 50

Crucially, these look essentially equivalent to normal if-else-statements in these languages.

[–] [email protected] 3 points 1 year ago

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.

[–] GTG3000 1 points 1 year ago (2 children)

and then you have lua

local result = condition and a or b

I'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.

[–] [email protected] 1 points 1 year ago

Still less intuitive than Kotlin's if expressions.

[–] [email protected] 1 points 1 year ago

It's amazing what people have made in Roblox with that language.

[–] [email protected] 1 points 1 year ago

Swift just got this (for switch statements too), and my god is it great.

load more comments
view more: next ›