54 points

And no one on his team ever understood his code.

Sometimes being declarative is better than being “smart”

permalink
report
reply
33 points

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.

permalink
report
parent
reply
24 points

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?

permalink
report
parent
reply
0 points

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

permalink
report
parent
reply
7 points

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

permalink
report
parent
reply
4 points

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.

permalink
report
parent
reply
5 points

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.

permalink
report
parent
reply
30 points

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.

permalink
report
parent
reply
49 points

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.

permalink
report
parent
reply
11 points

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

permalink
report
parent
reply
7 points

?? Profit

permalink
report
parent
reply
13 points

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.

permalink
report
parent
reply
1 point

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

permalink
report
parent
reply
5 points
*

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.

permalink
report
parent
reply
0 points

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.

permalink
report
parent
reply
7 points

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.

permalink
report
parent
reply
0 points

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

permalink
report
parent
reply
4 points
*

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.

permalink
report
reply
0 points

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.

permalink
report
reply
-2 points

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.

permalink
report
reply
12 points

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

permalink
report
reply
3 points

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

permalink
report
parent
reply
4 points

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

permalink
report
parent
reply
1 point

10 goto 20

20 goto 10

permalink
report
parent
reply
10 points

But I really need to go.

permalink
report
parent
reply
1 point

Then use golang

permalink
report
parent
reply
1 point

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

permalink
report
parent
reply
3 points

#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

permalink
report
parent
reply
1 point

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

permalink
report
parent
reply

Programmer Humor

!programmerhumor@lemmy.ml

Create post

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.

Community stats

  • 4.4K

    Monthly active users

  • 1.5K

    Posts

  • 35K

    Comments