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

Programmer Humor

32745 readers
104 users here now

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

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 54 points 1 year ago (7 children)

And no one on his team ever understood his code.

Sometimes being declarative is better than being "smart"

[–] [email protected] 49 points 1 year 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 1 year 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.

[–] [email protected] 7 points 1 year ago
[–] [email protected] 34 points 1 year 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 1 year 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 1 year ago (2 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?

[–] [email protected] 4 points 1 year ago (1 children)

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.

[–] [email protected] 6 points 1 year ago (1 children)

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.

[–] [email protected] 2 points 1 year ago* (last edited 1 year ago)

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.

[–] [email protected] 0 points 1 year ago (1 children)

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

[–] [email protected] 7 points 1 year ago

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

[–] [email protected] 13 points 1 year 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.

[–] [email protected] 5 points 1 year ago* (last edited 1 year ago) (2 children)

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.

[–] [email protected] 7 points 1 year ago (1 children)

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.

[–] [email protected] 0 points 1 year ago (1 children)

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

[–] [email protected] 4 points 1 year ago

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.

[–] [email protected] 0 points 1 year ago

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.

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

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