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
Programmer Humor
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.
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
Sounds delightful. I'm sure that nothing is explained at length repeatedly in a 35 minute meeting that could have been a message
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?
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.
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
.
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.
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.
I enjoy this:
return a.or(b);
But yeah, that requires an Option
type rather than null
pointers...
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.
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).
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.
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.
PHP too
Python, checking in ...
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.
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