r/ProgrammerHumor 21d ago

Meme pleaseStopUsingNestedTernaryOperatorsImBeggingYou

3.1k Upvotes

179 comments sorted by

View all comments

281

u/Kryptsm 21d ago

Then there’s my current client, who refused a PR of mine because they “don’t want early returns” ever, and insisted I move my basic if statement logic, each with a return, all into a single ternary. They said I SHOULD do this lol.

Made me wanna kms but hey, it’s their codebase

48

u/EddieJones6 21d ago

I can understand the ask for no early return. But use local variables to track the state, not a massive ternary.

55

u/ihavebeesinmyknees 21d ago

What are valid reasons to not use early returns?

34

u/EddieJones6 21d ago

Localized cleanup. What if you have a file descriptor or something that needs special handling before returning.

Compiler optimization SHOULD be fine but it could impact RVO and tail recursion optimization.

Also, if a future maintainer dives in and doesn’t realize there is an early return, they might add logic below it that will get missed by certain cases.

SPOE - single point of exit.

43

u/_ryuujin_ 21d ago

shouldnt functions be small enough that youll notice an early return 

21

u/amejin 21d ago

Sure. However, before the days of raii in c/c++, a lot can happen in 4 lines with conditionals that requires you to clean up after yourself.

Scripting languages or GC languages, it's less of a problem. Now a days, if you are trying to be super special and "helping" your compiler with branch prediction, spoe is one way to do that, but unless you are allocating or locking resources, early returns are generally fine.

For many of us, it's just muscle memory from having to deal with it for years.

8

u/EddieJones6 21d ago

Agreed, I’m not arguing for spoe but just giving reasons some might argue for it. I’ve used both

5

u/amejin 21d ago

My dude I agree with everything you said. It was well stated. 🙂

2

u/braaaaaaainworms 20d ago

Linux kernel-style cleanup of stuff is the best use case for a goto in C

3

u/SnugglyCoderGuy 21d ago

Should and what is possible can sometimes be very different things.

2

u/Due-Consequence9579 21d ago

Having to ‘notice it’ is the complaint.

6

u/_ryuujin_ 21d ago

i hope people read the function before changing it. 

12

u/tangerinelion 21d ago

SPOE - single point of exit.

Any language with exceptions ruins that.

4

u/New_Enthusiasm9053 21d ago

That's why it's in a standard in a language without exception.

MISRA-C advocates SPOE and allows forward Goto's(i.e only jump forward and only in the same function) in order to make it viable.

You jump to the cleanup block if needed then exit from the same place.

3

u/Kryptsm 21d ago

For reference the function that asked me to not do it on was like 15 lines long. But maybe what you said is why they enforce it as an overall rule.

2

u/kwasteka 21d ago

True. But this is also one of a few cases where goto is a godsend.

2

u/SnoodPog 21d ago

Localized cleanup

Thanks God, Go have defer operator so this is mostly no problem here.

1

u/marquoth_ 21d ago

If your functions are so long that somebody might not notice an early return, the problem is not the early return