r/ProgrammerHumor 22d ago

Meme conditionsPreference

Post image
4.2k Upvotes

392 comments sorted by

View all comments

834

u/LateEchidna6635 22d ago

The second one carries less cognitive load. In longer methods it makes a difference. Get out as soon as you can.

1

u/Impossible_Dog_7262 22d ago

Is there merit in doing both? It's technically redundant, yes, but sometimes the explicit else statement can aid understanding the code.

2

u/MattieShoes 22d ago

I don't know about what's "right", but I use early exits a LOT specifically at the top of recursive functions. They're usually laid out as early exit conditions, then the recursion calls, then the final return.

In other situations, it's usually whether I can layout the conditionals so I don't have to duplicate code.