this post was submitted on 08 Dec 2023
0 points (NaN% liked)

Programmer Humor

31997 readers
794 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
top 25 comments
sorted by: hot top controversial new old
[–] DudeDudenson@lemmings.world 0 points 9 months ago (1 children)

And no one on his team ever understood his code.

Sometimes being declarative is better than being "smart"

[–] KairuByte@lemmy.dbzer0.com 0 points 9 months ago (1 children)

I’m confused on how this is difficult to understand. Put aside the fact that it’s just a regular operator that… I mean virtually everyone should know, how hard is it to google “what does ?? mean in [language]” which has the added benefit of learning a new operator that can clean up your code?

[–] DonnerWolfBach@feddit.de 0 points 9 months ago

Well yeah but imagine you had to do that on most lines of the code? It would become very distracting imho. If you are in a team with people that have a lot experience and or will learn more anyway this is fine. But if you are in a team with not very good programmers which "will never learn" because they have other stuff to do, you should be careful when using code like this. Though I would prefer in the former of course.

[–] Knusper@feddit.de 0 points 9 months ago (1 children)

I enjoy this:

return a.or(b);

But yeah, that requires an Option type rather than null pointers...

[–] mea_rah@lemmy.world 0 points 9 months ago (1 children)

Is that Rust? Assuming a is an Option (which would be close approximation of OP's nullable type) and assuming b is not null, then this would be closer to the original idea:

a.unwrap_or(b)

It returns value of a if it's not None rather than Option.

[–] Knusper@feddit.de 0 points 9 months ago

Ah, true. Thanks.

Theoretically, it was supposed to be pseudo-code, secretly inspired by Rust, but I did get that one mixed up.

And I am actually even a fan of the word unwrap there, because it follows a schema and you can have your IDE auto-complete all the things you can do with an Option.
In many of these other languages, you just get weird collections of symbols which you basically have to memorize and which only cover rather specific uses.

[–] zero_gravitas@aussie.zone 0 points 9 months ago (1 children)

There's a nice list of this feature by language on the Wikipedia page for anyone interested: https://en.wikipedia.org/wiki/Null_coalescing_operator#Examples_by_languages

[–] meliaesc@lemmy.world 0 points 9 months ago (2 children)

This was my first time actually seeing a Rust example, and I hate it.

[–] Flipper@feddit.de 0 points 9 months ago

Other languages: if a is null return b.

Rust: here is an array of strings, we are going to parse the array to numbers. If that conversion fails we handle the exception and return the minimum integer value. We then save the result in a new vector. We also print it.

I like rust, but I hate the example too. It's needlessly complex. Should have just been a.unwrap_or(b).

[–] xor@lemmy.blahaj.zone 0 points 9 months ago (1 children)

You'll be happy to hear I've updated the example to be not bad

[–] Username@feddit.de 0 points 9 months ago* (last edited 9 months ago) (1 children)

I wanted to ask why it's bad, what did you change?

Btw. the example function get_default is badly chosen, because unwrap_or_default exists.

[–] xor@lemmy.blahaj.zone 0 points 9 months ago (1 children)

The original example was doing the unwrap_within an iterator doing some string parsing, so there was a lot of unrelated boilerplate around the actual unwrapping that made it really unclear, as well as usual unwrap_or_else to produce a constant value

Ehhh, I was more using get_default as a placeholder for some function, as opposed to representing Default::default for the inner type specifically. I think it should be alright since only people familiar with rust would know about the default trait anyway. I did consider adding an unwrap_or_default example, but thought it was getting a bit off topic at that point.

[–] Username@feddit.de 0 points 9 months ago

Yeah I get it, it was just something I noticed. A pedantic lint, you could say.

[–] zero_gravitas@aussie.zone 0 points 9 months ago* (last edited 9 months ago) (1 children)

Ruby:

a || b

(no return as last line is returned implicitly, no semicolon)

EDIT: As pointed out in the comments, this is not strictly equivalent, as it will return b if a is false as well as if it's nil (these are the only two falsy values in Ruby).

[–] stebo02@sopuli.xyz 0 points 9 months ago (1 children)

Python:

return a or b

i like it because it reads like a sentence so it somewhat makes sense

and you can make it more comprehensive if you want to:

return a if a is not None else b

[–] Turun@feddit.de 0 points 9 months ago* (last edited 9 months ago)

This diverges from the OP code snippets if a has the value False.

[–] cerement@slrpnk.net 0 points 9 months ago* (last edited 9 months ago) (1 children)

~~return a and a or b~~ → return a or b

correction from @murtaza64

[–] DmMacniel@feddit.de 0 points 9 months ago

Lua. My beloved.

[–] PoastRotato@lemmy.world 0 points 9 months ago (1 children)

My coworker flips his shit every time I include a ternary operator in a PR. He also insists on refactoring any block of code longer than two lines into its own function, even when it's only used once.

He is not well liked.

[–] bort@feddit.de 0 points 9 months ago

He also insists on refactoring any block of code longer than two lines into its own function

Thanks, uncle Bob.

[–] 30p87@feddit.de 0 points 9 months ago

Except there's literally no change in performance as a normal compiler will treat those the same. It just looks nice and trim down the time an experienced dev reads and understands the code by around 200ms.

[–] bamboo@lemm.ee 0 points 9 months ago (1 children)

I’m learning swift and I actually just discovered ?? today. Am I missing out in other languages?

[–] hstde@feddit.de 0 points 9 months ago (1 children)

Yes, it's very useful when applied correctly.

I'm always disappointed when I remember, that I can't use such a feature, because I'm stuck using Java.

[–] jape@infosec.pub 0 points 9 months ago (1 children)

Maybe you'r using an older version of Java? Works fine for me.

[–] hstde@feddit.de 0 points 9 months ago* (last edited 9 months ago)

New versions of java have a null coalescing operator?

I didn't know that.

Edit: a short search didn't return any answers, as far as I can see java doesn't have this operator, the closest thing is the ternary if operator.