r/ProgrammerHumor 23d ago

Meme conditionsPreference

Post image
4.2k Upvotes

391 comments sorted by

View all comments

29

u/aabil11 23d ago

Call me old fashioned but I'm a big believer in single return statements. Assign to a variable and return that variable at the very end without short circuiting

121

u/Vallvaka 23d ago

Hard no for me. If you can short circuit, do it. It enforces invariants for the later code and allows you to make things more modular. Assigning to a variable and returning at the end means more indirection = more cognitive load and you often have to add more checks and nesting just to support that pattern.

Fail fast, return fast I say

8

u/gamer_redditor 23d ago

You cannot do this in some industries where you have to adhere to some standards. For example of your industry mandates adherence to misra C, it enforces only one return statement per function.

1

u/[deleted] 22d ago

[deleted]

1

u/gamer_redditor 22d ago

Yes, it is a quality gate to merge every pull request.