9 points

I work with python so here’s python

return a or b

permalink
report
reply
4 points

I work with lisp so here’s lisp

(or a b)
permalink
report
parent
reply
28 points

a.unwrap_or(b)

permalink
report
reply
1 point

I tried picking up rust for the AoC, but any program I wrote ended up unreadable cuz of this unwrap_or. It just allows too much chaining. Then again other options for chaining operations aren’t much better, like match. Idk what I’m doing wrong or if rust never was meant to be readable.

permalink
report
parent
reply
12 points

🦀

permalink
report
parent
reply
10 points

Yea uh is this actually equivalent? In all of those other cases you’re checking if a is null and in the last case my understanding is it is checking to see if a is falsely. In the case that a is 0, or undefined, or an empty array or any other kind of non null falsey value, then the behavior would be different.

permalink
report
reply
13 points

Even in Javascript, the ?? operator checks explicitly for null or undefined. So it added undefined, but not 0 or false. But adding undefined sounds like a good addition for this operator.

See the Javascript section of: https://wikipedia.org/wiki/Null_coalescing_operator#Examples_by_languages

permalink
report
parent
reply
21 points

In C# that last one is the null propagation operator. If a is not null then a, else b.

permalink
report
parent
reply
7 points

Ah interesting one of those cases where this could be one of a few languages. I was reading it as JS.

permalink
report
parent
reply
3 points

I thought it was TS/JS too, but the way those braces are below the if statements makes it feel more like C#.

permalink
report
parent
reply
54 points

And no one on his team ever understood his code.

Sometimes being declarative is better than being “smart”

permalink
report
reply
1 point

Yes! Please be declaritive for the next people in line!

permalink
report
parent
reply
24 points

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?

permalink
report
parent
reply
4 points

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.

permalink
report
parent
reply
5 points

Honestly, and I mean this sincerely, if you’re on a team where the nullable coalesce is going to be confusing after the first handful of times encountered… look for a new job. It doesn’t bode well for their ability to do their jobs.

This is like the guy at Walmart who needs hand holding each time they clean a machine, it’s a problem waiting to happen.

permalink
report
parent
reply
0 points

If condition then this else that vs this ?? that

Which option do you think requires less time for a person to identify and understand?

Sure if it’s just your own code do whatever comes natural to you but there’s a reason we don’t use these kind of logical operators in day to day speech is my point.

Ive been a backend dev for 2 years now and I’ve never come across the ?? operator and every time I come across a ternary operator I have to Google in what order comes what.

Not saying it doesn’t make the code more concise and less “noisy” but sometimes a simple if else statement just makes the code easier to mantain

permalink
report
parent
reply
7 points

It’s easier to mess up return a != null ? a : b than it is return a ?? b, and operators work from left to right.

permalink
report
parent
reply
13 points

This is why I favor 3. It’s fairly concise while not being all that obscure. And even if you’re not 100% on that syntax, context provides decent intuition about what it does.

permalink
report
parent
reply
5 points
*

This is why I usually don’t comment on stuff like this in PRs. If it’s readable and easy to understand it doesn’t need more abstractions. Even if it’s less code. What’s it save like a few bytes? That’s not as useful as the whole team instantly knowing how the code works when they see it lol

I will say though if a jr dev came upon the last code they would just look it up and learn something so that’s a total valid path too. Just depends on your codebase and how your team works. I think it usually ends up being a mix with larger teams.

permalink
report
parent
reply
7 points

There’s more to it imho. The first three are more prone to mistakes than the last. You are much less likely to accidentally alter the logic intended in a simple null coalesce than you are in if statements.

permalink
report
parent
reply
0 points

That’s fair but if you had proper test coverage there wouldn’t be much risk. Who has that though? Lol

permalink
report
parent
reply
0 points

Yeah, I think there is a tipping point between terse and magic. I might grimace a little at the first one, have no comment on the middle two, and definitely comment on the last one. Wrote code like the person troubleshooting it is on-call, mildly hung over, and it’s 3am.

permalink
report
parent
reply
33 points

Sure, if the rest of the team is first semester CS students doing their first group project. This is not an obscure 1337 h4x0r trick only known to programming gods writing COBOL code inside banking mainframes, it’s a simple operator.

permalink
report
parent
reply
49 points

The last panel is infinitely more readable than parsing the whole chunk of logic above. Maybe you’re just not used to this language’s (I think this meme used C#) null operators.

permalink
report
parent
reply
11 points

Yeah, I have very little programming experience, and even not knowing the code, I figured this one out. Super simplified and clear.

permalink
report
parent
reply
7 points

?? Profit

permalink
report
parent
reply
30 points

Sure, but null coalescing is a pretty common feature in modern languages. Once you know what ?? means you can apply it to a whole host of languages.

permalink
report
parent
reply
6 points

What if both are null

permalink
report
reply
13 points

Normally the purpose of a block of code like this is to provide a fallback hardcoded value if the dynamic value from your API or whatever is null. Like, setting a default title for a page of a notes app if the user didnt set a title themselves etc.

So, b is very likely to be a never-null, hardcoded value.

This code can still be valid and return null if b is null too, as the other person said.

permalink
report
parent
reply
33 points

Then null will be returned, as the value of b.

permalink
report
parent
reply
1 point

What is null is null?

permalink
report
parent
reply
5 points

True

permalink
report
parent
reply

Programmer Humor

!programmerhumor@lemmy.ml

Create post

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

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.

Community stats

  • 4.4K

    Monthly active users

  • 1.5K

    Posts

  • 35K

    Comments