82 points

People ITT hating on null coalescing operators need to touch grass. Null coalescing and null conditional (string?.Trim()) are immensely useful and quite readable. One only has to be remotely conscious of edge cases where they can impair readability, which is true of every syntax feature

permalink
report
reply
-16 points

Languages with null in them at all anymore just irk me. It’s 2023. Why are we still giving ourselves footguns.

permalink
report
parent
reply
38 points

Because I use a language that was invented more than 1 year ago

permalink
report
parent
reply
10 points

and it feeds me.

permalink
report
parent
reply
2 points

You use the language? Weren’t they just for bragging rights and blog posts?

permalink
report
parent
reply
16 points
*

Because languages need to be able to handle the very common edge cases where data sources don’t return complete data.

Adding null coalescing to a null-safe language (like dart) is so much easier to read and infer the risk of handling null than older languages that just panic the moment null is introduced unexpectedly.

permalink
report
parent
reply
7 points

For old languages, null coalescing is a great thing for readability. But in general null is a bad concept, and I don’t see a reason why new languages should use it. That, of course, doesn’t change the fact that we need to deal with the nulls we already have.

permalink
report
parent
reply
2 points
*

Who said anything about panicking the minute we encounter incomplete data? Just do what Rust does and, instead of having all types be able to be null, statically enforce that all variables have an initialized value and have a value have a separate type Option<T> which can either be Some(T) or None, and have the compiler not let you access the value inside unless you write code to handle the None case. There are standard library helper functions for common operations like null coalescing and, as you say, panicking when you encounter a null, but you have to explicitly tell the compiler you want to do that by calling myOption.unwrap()

What makes this really cool is that you can have an Option<Option<T>> where Some(None) is not the same as None, so an iterator that signals end of list by returning None can have None elements in it.

Say what you will about the functional programming people but they were spot on with this one. Having an Option monad in place of the ability for null is absolutely the way to go. I’d say it’s the future but Lisp and APL had this figured out in the 60s

permalink
report
parent
reply
3 points

Because you can turn null into an Option monad with a small amount of syntax sugar and static analysis

permalink
report
parent
reply
65 points

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.

permalink
report
reply
36 points

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

Thanks, uncle Bob.

permalink
report
parent
reply
18 points
*

His advice is great for newer programmers. They are taken literally by newer programmers, but the goal is not to force the dogma onto everyone. Maybe that should be more clear before the new people make a fool of themselves. They’ll learn why or how to apply these rules once they get more experience.

I know the episode you’re referring to and the important part is to realize you can use functions names/signatures to convey and structure information about your code, not just as a way to reuse code. This is often misunderstood by newer programmers, self-taught programmers. Your code should be easy to understand so it’s up to us to make it well structured. Functions aren’t only to avoid duplicate code

permalink
report
parent
reply
5 points

Then refactor those two freshly-refactored lines into their own function as well for polymorphism, right?

permalink
report
parent
reply
15 points

Sounds delightful. I’m sure that nothing is explained at length repeatedly in a 35 minute meeting that could have been a message

permalink
report
parent
reply
3 points

Sounds like they learned programming from heavily object oriented languages.

As far as ternary operators go, I personally find them less readable than the longer if else format, but with any reasonably modern IDE or git setup that should be just a simple pass of an auto-formatting tool to convert them to the project/team standard. No need for any team friction. That should be automatically handled before it goes to review.

I’m not in a position with a PR process or anything like that (I’m a glorified scripter in over his head where we need a dev team for internal tools) but I struggle with over reliance on functions as well.

To keep from going overboard I always try to evaluate how often the block of code will see re-use (want to eliminate copy/paste and the footgun of forgetting to update a copy somewhere), how useful it would be to maintainers to have that section abstracted away into a black box (so you only have to worry about the part of the code that’s not working instead of the whole damn thing when something breaks), and how likely that block of code may need to be completely replaced later (if it’s separated out into a function, it’s a discrete chunk where we only have to maintain the same input and output formatting and ideally the rest of the program just werks).

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
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
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
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
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
1 point

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

permalink
report
parent
reply
49 points
*

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).

permalink
report
reply
22 points

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

permalink
report
parent
reply
14 points
*

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

permalink
report
parent
reply
7 points

I personally dislike this because when you read “or” you expect some boolean result not a random object :/

permalink
report
parent
reply
4 points

there’s always the second option for you

permalink
report
parent
reply
1 point

For newer python people, they see return a or b and typically think it returns a boolean if either is True. Nope. Returns a if a is truthy and then checks if b is truthy. If neither are truthy, it returns b.

permalink
report
parent
reply
7 points
*

Returns a if a is truthy and then checks if b is truthy. If neither are truthy, it returns b.

Not quite. If a is not truthy, then the expression a or b will always return b.

So, there is never any reason to check the truthiness of b.

you can paste this in your repl to confirm it does not.
class C:
 def __repr__(self): return [k for k, v in globals().items() if v is self][0]
 def __bool__(self):
  print(f"{self}.__bool__() was called")
  return False

a, b = C(), C()
print(f"result: {a or b}")
output
a.__bool__() was called
result: b
permalink
report
parent
reply
13 points

This doesn’t work for booleans because false is not null but also not truthy. One of things I hate about ruby is that it doesn’t have a native null coalescing operator.

permalink
report
parent
reply
1 point

Yeah, you’re quite correct, it’s not exactly equivalent, I just went on auto-pilot because it’s used so much for that purpose 🤖

It’s much closer to being a true null-coalescing operator than ‘OR’ operators in other languages though, because there’s only two values that are falsy in Ruby: nil and false. Some other languages treat 0 and "" (and no doubt other things), as falsy. So this is probably the reason Ruby has never added a true null-coalescing operator, there’s just much fewer cases where there’s a difference.

It’s going to drive me mad now I’ve seen it, though 😆 That’s usually the case with language features, though, you don’t know what you’re missing until you see it in some other language!

permalink
report
parent
reply
7 points
*

Perl has both $a || $b and $a // $b.

The || version is older and has the value of $b if $a is any false value including undef (which is pretty much Perl’s null/nil).

The // version has the value of $b iff $a is undef. Other “false” values carry through.

Ruby took both “no return required” and “no final semicolon required” from Perl (if not a few other things), I think, but it seems that // was Perl later borrowing Ruby’s || semantics. Interesting.

i.e. 0 || 1 is 1 in Perl but 0 in Ruby. Perl can 0 // 1 instead if the 0, which is a defined value, needs to pass through.

permalink
report
parent
reply

Typescript/Javascript too

permalink
report
parent
reply
17 points
*

Not strictly equivalent, since || tests for truthiness, not just null.

permalink
report
parent
reply

That’s fair, but it’s close enough that it functions identically

permalink
report
parent
reply
33 points

I enjoy this:

return a.or(b);

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

permalink
report
reply
6 points

a?.or(b)

Kotlin go brr

permalink
report
parent
reply
9 points

a ?: b

permalink
report
parent
reply
5 points

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.

permalink
report
parent
reply
1 point

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.

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