know the features of your language
know the features of your language
know the features of your language
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
Languages with null in them at all anymore just irk me. It's 2023. Why are we still giving ourselves footguns.
Because I use a language that was invented more than 1 year ago
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.
Because you can turn null into an Option monad with a small amount of syntax sugar and static analysis
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.
He also insists on refactoring any block of code longer than two lines into its own function
Thanks, uncle Bob.
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
Then refactor those two freshly-refactored lines into their own function as well for polymorphism, right?
Sounds delightful. I'm sure that nothing is explained at length repeatedly in a 35 minute meeting that could have been a message
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).
And no one on his team ever understood his code.
Sometimes being declarative is better than being "smart"
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.
Yeah, I have very little programming experience, and even not knowing the code, I figured this one out. Super simplified and clear.
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.
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.
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?
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.
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
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.
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.
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.
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.
Yes! Please be declaritive for the next people in line!
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).
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
This diverges from the OP code snippets if a has the value False
.
I personally dislike this because when you read "or" you expect some boolean result not a random object :/
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.
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.
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!
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.
Typescript/Javascript too
Not strictly equivalent, since ||
tests for truthiness, not just null.
I enjoy this:
undefined
return a.or(b);
But yeah, that requires an Option
type rather than null
pointers...
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:
undefined
a.unwrap_or(b)
It returns value of a
if it's not None rather than Option.
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.
a?.or(b)
Kotlin go brr
a ?: b
a.unwrap_or(b)
🦀
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.
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
This was my first time actually seeing a Rust example, and I hate it.
You'll be happy to hear I've updated the example to be not bad
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).
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.
Damn, they really just made that example as ugly as possible huh
(or a b)
i never thought of lisp as concise before
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
Practical, but not really equivalent though because of nil punning.
Loads of beginners in this thread. Here's how it's done in the industry.
The code:
return a;
The test:
a = rand()%100+1;
It works, boss.
This must be a custom random function, because it's standard for random to return a float between 0-1 exclusive. Maybe you meant to multiply by 100 instead of modulo.
I think for once I didn't fuck up.
I’m learning swift and I actually just discovered ??
today. Am I missing out in other languages?
C# and Kotlin both have it
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.
Maybe you'r using an older version of Java? Works fine for me.
PHP too
JavaScript and TypeScript too
C#
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.
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))
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.
Inb4 the JavaScript fanboys appear and argue a bad tool is fine if you're a genius, actually. Why aren't you a genius?
Nullish coalescing makes a lot of stuff much easier to read and write.
Python, checking in ...
python
return (a or b)
Parentheses aren't necessary, I just prefer them for readability.
See python documentation on boolean operators for reference. Short story is a or b
is an expression that evaluates to a
if a
is "truthy" else b
. "Falsy" is empty strings/containers, 0
and None
.
You need to be careful here though. You might not intend for 0
and None
to mean the same thing.
So this won't do the intended thing if a is 0.
Edit: Sorry I meant to reply to the parent comment, realising now you already write the exact same thing.
There was PEP 505 to add a none-aware operator, but it lost support.
That’s a shame, it would have been fitting in “modern” Python along with the walrus and static type system.
→ return a and a or b
return a or b
correction from @murtaza64
Lua. My beloved.
That's valid Python as well
You don't need the and right? Can't it just be return a or b
This doesn't work if a is falsy non-null actually
it’s just return a and b or c
is the closest Lua has to a ternary operator, but yes, for the above you could shorten it to return a or b
(“or
returns the first argument if true, otherwise second argument is returned”)
Please don't use #2. It is how you get the goto fail bug
But I really need to go.
Then use golang
Should you even be using goto? I was taught to avoid it like the plague
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
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
#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
10 goto 20
20 goto 10
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.
In C# that last one is the null propagation operator. If a is not null then a, else b.
Ah interesting one of those cases where this could be one of a few languages. I was reading it as JS.
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
I work with python so here's python
return a or b
I work with lisp so here's lisp
undefined
(or a b)
What if both are null
Then null will be returned, as the value of b.
What is null is null?
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.
fromMaybe a b
we can remove the return!
js
const fn = (a, b) => a || b
undefined
const fn = (a, b) => a ?? b
Gotta love some peer review
But this just creates a function. You still have to call it.
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.
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.
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.