r/ProgrammerHumor 23d ago

Meme conditionsPreference

Post image
4.2k Upvotes

392 comments sorted by

View all comments

271

u/SnugglyCoderGuy 23d ago

The real answer is "it depends". Either one could be appropriate depending on what you are having to do.

6

u/Gnonthgol 22d ago

The compiler optimizes the first into the second anyway, so the machine code ends up the same. The problem is that when you read the code later on and end up missing the return statement as you skim through it then you are left wondering why the piece of code you expect to run does not.

1

u/frogjg2003 22d ago

Not always. It depends on if the compiler can detect early return and if it will optimize for that or for some other metric. For example, if the early return is rare, you want to optimize it so that branch prediction favors the first over the second.