DudeDudenson,

And no one on his team ever understood his code.

Sometimes being declarative is better than being “smart”

RagingRobot,

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.

jhulten,

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.

KairuByte,
@KairuByte@lemmy.dbzer0.com avatar

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.

RagingRobot,

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

KairuByte,
@KairuByte@lemmy.dbzer0.com avatar

None of my projects had time for reliable testing unfortunately. It was always “next sprint” or “when we have time” which never really came to fruition.

saumanahaii,

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.

merthyr1831,

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.

PixxlMan,

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.

Thermal_shocked,

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

DudeDudenson,

?? Profit

KairuByte,
@KairuByte@lemmy.dbzer0.com avatar

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,

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.

KairuByte,
@KairuByte@lemmy.dbzer0.com avatar

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.

Zangoose,

Imo it’s context dependent. Obligatory “I’m only a college student/intern” out of the way.

Whenever I’m working with a project with multiple languages (e.g. split frontend+backend, different connected services, etc.) operators like that can get blurry when they aren’t consistent between lancuages. Especially when one of those languages doesn’t have runtime type enforcement or has weird boolean behavior (looking at you JS/TS) which can lead to unintended behavior

If everyone on the project is only working with that language, then your point is probably pretty close to the mark.

DudeDudenson,

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

KairuByte,
@KairuByte@lemmy.dbzer0.com avatar

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

herrvogel,

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.

RubberElectrons,
@RubberElectrons@lemmy.world avatar

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

rimorso,

I work with python so here’s python

return a or b

tetris11,
@tetris11@lemmy.ml avatar

I work with lisp so here’s lisp


<span style="color:#323232;">(or a b)
</span>
sndrtj,

a.unwrap_or(b)

itslilith,
@itslilith@lemmy.blahaj.zone avatar

🦀

FierySpectre,

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.

ABC123itsEASY,

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.

Ebber,

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

ABC123itsEASY,

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

dukk,

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

darkpanda,

K&R for life

Rednax,

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: wikipedia.org/wiki/Null_coalescing_operator#Examp…

shalva97,

What if both are null

Spzi,

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

15liam20,

What is null is null?

RagingRobot,

True

merthyr1831,

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.

zero_gravitas,

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

meliaesc,

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

hansl,

The Option type would have been a better example, and make it slightly less complicated.

Option is an enum with two variants; None and Some(T). You can chain Options with operations, describing a Monad chain, which is kind of what this meme represent.

Flipper,

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

mea_rah,

The example even used unwrap_or_else where they should use unwrap_or. Then it uses std::i64::MIN as fallback value where they could use something like 0 that would be a better example and honestly make more sense there.


<span style="color:#323232;">let parsed_numbers = ["1", "not a number", "3"]
</span><span style="color:#323232;">    .iter()
</span><span style="color:#323232;">    .map(|n| n.parse().unwrap_or(0))
</span><span style="color:#323232;">    .collect();
</span><span style="color:#323232;">
</span><span style="color:#323232;">// prints "[1, 0, 3]"
</span><span style="color:#323232;">println!("{:?}", parsed_numbers);
</span>

Even without trimming this to something less convoluted, the same functionality (with different fallback value) could be written in more readable form.

Obviously in the context of the page something like this would make way more sense:


<span style="color:#323232;">maybe_number.unwrap_or(0)
</span>

Or perhaps more idiomatic version of the above:


<span style="color:#323232;">maybe_number.unwrap_or_default()
</span>
words_number,

I think you could even get rid of the iter() and the collect() since it’s a small fixed size array.

barsoap,

Yep [T;N] has a direct implementation of . “{:?}” is necessary because arrays aren’t Display but you could get around that by saying


<span style="color:#323232;">[</span><span style="color:#183691;">"1"</span><span style="color:#323232;">, </span><span style="color:#183691;">"not a number"</span><span style="color:#323232;">, </span><span style="color:#183691;">"3"</span><span style="color:#323232;">].</span><span style="color:#62a35c;">map</span><span style="color:#323232;">(|n| println!(</span><span style="color:#183691;">"</span><span style="color:#0086b3;">{}</span><span style="color:#183691;">"</span><span style="color:#323232;">, n.</span><span style="color:#62a35c;">parse</span><span style="color:#323232;">().</span><span style="color:#62a35c;">unwrap_or</span><span style="color:#323232;">(</span><span style="color:#0086b3;">0</span><span style="color:#323232;">)));
</span>

but now I’m golfing. Also


<span style="font-weight:bold;color:#a71d5d;">for</span><span style="color:#323232;"> n </span><span style="font-weight:bold;color:#a71d5d;">in </span><span style="color:#323232;">[</span><span style="color:#183691;">"1"</span><span style="color:#323232;">, </span><span style="color:#183691;">"not a number"</span><span style="color:#323232;">, </span><span style="color:#183691;">"3"</span><span style="color:#323232;">] {
</span><span style="color:#323232;">   println!(</span><span style="color:#183691;">"</span><span style="color:#0086b3;">{}</span><span style="color:#183691;">"</span><span style="color:#323232;">, n.</span><span style="color:#62a35c;">parse</span><span style="color:#323232;">().</span><span style="color:#62a35c;">unwrap_or</span><span style="color:#323232;">(</span><span style="color:#0086b3;">0</span><span style="color:#323232;">))
</span><span style="color:#323232;">}
</span>

is more idiomatic I shouldn’t let my Haskell get the better of me. That does use Iterator, not that it makes a difference here.

xor,

Damn, they really just made that example as ugly as possible huh

xor,

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

Username,

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.

meliaesc,
barsoap,

<span style="color:#323232;"> .map(|n| n.parse().unwrap_or_else(|_| std::i64::MIN))
</span>

shudder

unwrap_or_else and passing a closure that ignores its argument is a definitive smell. Probably gets caught by clippy.

xor,

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,

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

xor,

Ayyyyy

barsoap,

Maybe shouldn’t call the function anything default and also provide it. The std docs say:


<span style="color:#323232;">fn count(x: &amp;str) -> usize { x.len() }
</span><span style="color:#323232;">
</span><span style="color:#323232;">assert_eq!(Ok(2).unwrap_or_else(count), 2);
</span><span style="color:#323232;">assert_eq!(Err("foo").unwrap_or_else(count), 3);
</span>
nigh7y,

we can remove the return!


<span style="font-weight:bold;color:#a71d5d;">const </span><span style="font-weight:bold;color:#795da3;">fn </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#323232;">(a, b) </span><span style="font-weight:bold;color:#a71d5d;">=> </span><span style="color:#323232;">a </span><span style="font-weight:bold;color:#a71d5d;">|| </span><span style="color:#323232;">b
</span>
nick,

<span style="color:#323232;">const fn = (a, b) => a ?? b
</span>
shasta,

But this just creates a function. You still have to call it.

nick,

<span style="color:#323232;">((a, b) => a ?? b)();
</span>
nigh7y,

Gotta love some peer review

carpelbridgesyndrome,

Please don’t use #2. It is how you get the goto fail bug

UnrepententProcrastinator,

But I really need to go.

unique_hemp,

Then use golang

cupcakezealot,
@cupcakezealot@lemmy.blahaj.zone avatar

10 goto 20

20 goto 10

Uriel_Copy,

Should you even be using goto? I was taught to avoid it like the plague

carpelbridgesyndrome,

Apple wrote bugged TLS code that broke using unbraced ifs with a goto, hence the name “goto fail”. You don’t need a goto to break this code though. All you need is a second indented line under the if

camelbeard,

Can you explain? 1 and 2 seem like the same logic? Are they compiled differently?

To me number 2 is just the cleanest and most easy to read. But I really need to get more used to lambda’s

carpelbridgesyndrome,

#2 is also the most insideous to update. Add another indented line to one of the conditions and the cotrol flow completely breaks while visually appearing fine.

C and a number of other languages have annoying pair of parallel syntax systems that makes it easy for people to read code one way and computers to compile it another. People read the indentation and newlines while compilers count braces and semicolons. #2 gets rid of the braces and makes control flow driven by semicolons making human visual inspection more likely to fail

camelbeard,

Thanks for the reply (not really sure why someone would downvote you for answering a question).

mindbleach,

For the love of god, please do not use single-line alternatives to braced scopes. It’s only tolerable in lambdas like Array.map( v => v**2 ). It’s not for an implicit scope. It’s for evaluating a return value.

But holy shit is it nice to have ?. in Javascript. It’s such an anything-goes language until you look at it funny and it just shits the bed and silently dies. Hate hate haaate having to check if things exist, when so many other details fail politely and impact nothing. At least now it’s one extra character instead of try-catch rigmarole.

CanadaPlus,

Inb4 the JavaScript fanboys appear and argue a bad tool is fine if you’re a genius, actually. Why aren’t you a genius?

PoolloverNathan,

I’m fine with non-braced blocks, but they must always be on the same line as the parent statement (e.g. if (a != null) return a) to visually distinguish them. (inb4 argument about long conditions: they’d usually be spread out over several lines, and the statement would go on the closing parenthese (which is on a line by itself when split over multiple lines))

BehindTheBarrier,

We avoid that, because just at a glance you might not see the function flow change when returns are at the end of lines. It’s a minor thing of course.

kogasa,
@kogasa@programming.dev avatar

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

ferralcat,

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

Feathercrown,

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

lemmesay,
@lemmesay@discuss.tchncs.de avatar

and it feeds me.

riodoro1,

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

Feathercrown,

Oh yeah I forgot, first you have to make a blog post, then a devlog, then review the top 10 best features of JS es6 (9 years after it was released…). Then shitpost on social media network for the other half of the week, and boom! You’re officially a Master Programmer!

kogasa,
@kogasa@programming.dev avatar

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

merthyr1831,

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.

itslilith,
@itslilith@lemmy.blahaj.zone avatar

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.

wizardbeard,

How are we supposed to deal with null values though? It’s an important concept that we can’t eliminate without losing information and context about our data.

0 and “” (empty string/char) are very often not equivalent to null in my use cases and mean different things than it when I encounter them.

You could use special arbitrary values to indicate invalid data, but at that point you’re just doing null with extra steps right?

I’m really lost as to how the concept isn’t neccessary.

eeleech,

One alternative are monadic types like result or maybe, that can contain either a value or an error/no value.

itslilith,
@itslilith@lemmy.blahaj.zone avatar

you could take a look at what Rust is doing with the Option enum. Superficially it looks similar to using null, but it actually accomplishes something very different.

A function that classically would return a value, say an int, but sometimes returns null instead, becomes a function that returns an Option. This forces explicit handling of the two cases, namely Some(value) or None. This way, it is next to impossible to try to do an operation on a value that does not exist.

dukk,

I’ll point to how many functional languages handle it. You create a type Maybe a, where a can be whatever type you wish. The maybe type can either be Just x or Nothing, where x is a value of type a (usually the result). You can’t access the x value through Maybe: if you want to get the value inside the Maybe, you’ll have to handle both a case where we have a value(Just x) and don’t(Nothing). Alternatively, you could just pass this value through, “assuming” you have a value throughout, and return the result in another Maybe, where you’ll either return the result through a Just or a Nothing. These are just some ways we can use Maybe types to completely replace nulls. The biggest benefit is that it forces you to handle the case where Maybe is Nothing: with null, it’s easy to forget. Even in languages like Zig, the Maybe type is present, just hiding under a different guise.

If this explanation didn’t really make sense, that’s fine, perhaps the Rust Book can explain it better. If you’re willing to get your hands dirty with a little bit of Rust, I find this guide to also be quite nice.

TLDR: The Maybe monad is a much better alternative to nulls.

Feathercrown,

Isn’t a Maybe enum equivalent to just using a return value of, for example, int | null with type warnings?

itslilith,
@itslilith@lemmy.blahaj.zone avatar

Not quite, because the Maybe enum is neither int nor null, but it’s own, third thing. So before you can do any operations with the return value, you need to handle both cases that could occur

Feathercrown,

Isn’t that also true with compile-time type checking though? Eg. 0 + x where x is int|null would be detected? I don’t have much experience here so I could be wrong but I can’t think of a case where they’re not equivalent

itslilith,
@itslilith@lemmy.blahaj.zone avatar

Most languages that let you do ambiguous return types don’t do compile-time type checking, and vice versa. But if it’s actually implemented that way, then it’s logically equivalent, you’re right. Still, I prefer having things explicit

Feathercrown,

Yeah it’s nice to be able to see it

kogasa,
@kogasa@programming.dev avatar

Yes it is

AVincentInSpace, (edited )

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&lt;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

juli,

I hate it.

It’s like:

The tree is green, if it’s summer. If it’s summer, the tree is green.

I like the second much better.

blawsybogsy,

(or a b)

i never thought of lisp as concise before

TAVAR,

True for all the lisps without explicit false (in the others its more or less a technicality)

Gotta love though that when lisp is concise it does so without overloading syntax

bnaur,

Practical, but not really equivalent though because of nil punning.

heavyboots,
@heavyboots@lemmy.ml avatar

I hate this so much. Literally stopped using Perl and switched to PHP to get away from the “Look, ma! I can condense 6 comprehensible lines to one complete gibberish line that still works!” crowd.

I’m not saying I won’t use shorthand if/else format on very rare occasions where you have to do a bunch of different if else’s within your HTML for some reason, but in general, I try to avoid it.

ugo,

If this was cpp, clang-tidy would tell you “do not use else after return”

I don’t know how null works in swift, but assuming it coerces to bool I’d write


<span style="color:#323232;">if (a) return a;
</span><span style="color:#323232;">return b;
</span>
eutampieri,

It’s nil, and you can coalesce it with ??

30p87,

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.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • programmerhumor@lemmy.ml
  • DreamBathrooms
  • mdbf
  • ethstaker
  • magazineikmin
  • cubers
  • rosin
  • thenastyranch
  • Youngstown
  • osvaldo12
  • slotface
  • khanakhh
  • kavyap
  • InstantRegret
  • Durango
  • JUstTest
  • everett
  • tacticalgear
  • modclub
  • anitta
  • cisconetworking
  • tester
  • ngwrru68w68
  • GTA5RPClips
  • normalnudes
  • megavids
  • Leos
  • provamag3
  • lostlight
  • All magazines