this post was submitted on 08 Dec 2023
618 points (96.4% liked)

Programmer Humor

32356 readers
1179 users here now

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

Rules:

founded 5 years ago
MODERATORS
 
top 50 comments
sorted by: hot top controversial new old
[–] [email protected] 83 points 10 months ago (19 children)

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

load more comments (19 replies)
[–] [email protected] 66 points 11 months ago (3 children)

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.

[–] [email protected] 36 points 10 months ago (2 children)

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

Thanks, uncle Bob.

[–] [email protected] 18 points 10 months ago* (last edited 10 months ago)

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

load more comments (1 replies)
[–] [email protected] 15 points 11 months ago

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

load more comments (1 replies)
[–] [email protected] 54 points 10 months ago (11 children)

And no one on his team ever understood his code.

Sometimes being declarative is better than being "smart"

[–] [email protected] 49 points 10 months ago (1 children)

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.

[–] [email protected] 11 points 10 months ago (1 children)

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

load more comments (1 replies)
[–] [email protected] 34 points 10 months ago

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.

[–] [email protected] 30 points 10 months ago

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.

[–] [email protected] 24 points 10 months ago (5 children)

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?

load more comments (5 replies)
[–] [email protected] 13 points 10 months ago

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.

load more comments (6 replies)
[–] [email protected] 49 points 11 months ago* (last edited 10 months ago) (7 children)

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

[–] [email protected] 22 points 11 months ago (6 children)

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

[–] [email protected] 15 points 10 months ago* (last edited 10 months ago)

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

load more comments (5 replies)
[–] [email protected] 13 points 10 months ago (1 children)

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.

load more comments (1 replies)
load more comments (5 replies)
[–] [email protected] 32 points 11 months ago (4 children)

I enjoy this:

return a.or(b);

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

load more comments (4 replies)
[–] [email protected] 28 points 10 months ago (2 children)
[–] [email protected] 12 points 10 months ago
load more comments (1 replies)
[–] [email protected] 24 points 10 months ago (1 children)

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

[–] [email protected] 14 points 10 months ago (4 children)

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

[–] [email protected] 14 points 10 months ago (5 children)

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

load more comments (5 replies)
[–] [email protected] 12 points 10 months ago (2 children)

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

load more comments (2 replies)
load more comments (2 replies)
[–] [email protected] 17 points 11 months ago (3 children)

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.

load more comments (3 replies)
[–] [email protected] 17 points 11 months ago (5 children)

I’m learning swift and I actually just discovered ?? today. Am I missing out in other languages?

[–] [email protected] 17 points 11 months ago

C# and Kotlin both have it

[–] [email protected] 13 points 11 months ago (2 children)

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.

load more comments (2 replies)
[–] [email protected] 9 points 11 months ago
load more comments (2 replies)
[–] [email protected] 17 points 10 months ago* (last edited 10 months ago) (1 children)

(or a b)

i never thought of lisp as concise before

load more comments (1 replies)
[–] [email protected] 16 points 10 months ago (2 children)

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.

[–] [email protected] 9 points 10 months ago (1 children)

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

load more comments (1 replies)
load more comments (1 replies)
[–] [email protected] 14 points 11 months ago

Nullish coalescing makes a lot of stuff much easier to read and write.

[–] [email protected] 13 points 10 months ago (1 children)

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

if (a) return a;
return b;
load more comments (1 replies)
[–] [email protected] 12 points 10 months ago (7 children)

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

[–] [email protected] 10 points 10 months ago (1 children)
load more comments (1 replies)
load more comments (6 replies)
[–] [email protected] 12 points 11 months ago* (last edited 10 months ago) (5 children)

~~return a and a or b~~ → return a or b

correction from @murtaza64

load more comments (5 replies)
[–] [email protected] 12 points 11 months ago* (last edited 11 months ago) (3 children)

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.

[–] [email protected] 13 points 11 months ago (2 children)

You need to be careful here though. You might not intend for 0 and None to mean the same thing.

load more comments (2 replies)
load more comments (2 replies)
[–] [email protected] 10 points 10 months ago (2 children)

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.

[–] [email protected] 21 points 10 months ago (3 children)

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

load more comments (3 replies)
[–] [email protected] 13 points 10 months ago

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

load more comments
view more: next ›