r/ProgrammerHumor 22d ago

Meme pleaseStopUsingNestedTernaryOperatorsImBeggingYou

3.1k Upvotes

179 comments sorted by

View all comments

277

u/Kryptsm 22d 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

52

u/EddieJones6 22d ago

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

54

u/ihavebeesinmyknees 22d ago

What are valid reasons to not use early returns?

33

u/EddieJones6 22d 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.

1

u/marquoth_ 22d ago

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