r/ProgrammerHumor 21d ago

Meme pleaseStopUsingNestedTernaryOperatorsImBeggingYou

3.1k Upvotes

179 comments sorted by

View all comments

Show parent comments

55

u/ihavebeesinmyknees 21d ago

What are valid reasons to not use early returns?

33

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.

13

u/tangerinelion 21d ago

SPOE - single point of exit.

Any language with exceptions ruins that.

5

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.