why i believe early returns aren't that good
Posted by whitet_blacksp@reddit | programming | View on Reddit | 15 comments
Posted by whitet_blacksp@reddit | programming | View on Reddit | 15 comments
trackerstar@reddit
Suggesting GOTO in 2024 this is a bold move
barmic1212@reddit
Early return pattern exists to break the only one return rule. It's particularly interesting to validate the function parameters, before the function make the main thing.
Your example is other thing : how to handle errors (particularly in language that have only the return check to verify an error) and this have decades of debates.
edrenfro@reddit
I've seen code that followed this principle and it's a nightmare to read and maintain. Also, "but" not "butt."
Buttleston@reddit
Which principle? That early returns are good or bad?
edrenfro@reddit
Sorry, I was saying I've seen code that has deep nested if statements rather than early returns and it's very hard to read and maintain.
Buttleston@reddit
Ah yeah, I agree
Dustin-@reddit
If a section of code isn't readable when using early returns, the correct way to handle it is usually to rethink how you're using that function. You're correct that sandwiching if statements with early returns in between pieces of logic is a bad idea, but that likely means you need to refactor the function, either by rearranging the blocks or breaking out the different pieces into other functions.
Here's a very slightly modified version of your code with early returns:
Notice how all of the early error-checking if statements all happen in a row where it's easy to see that that's what that section of code does. The rest of the logic (including another if statement) is similarly grouped.
You can increase readability even more by noticing that this function has two responsibilities (opening and checking a file from a path as well as loading it into a buffer). Breaking these responsibilities into their own functions makes the code even more readable:
Now it's even more clear what your code does.
Importantly, this sort of refactor was only as easy as it was because of the early return breaks. In this example it would be pretty easy either way, but in larger, more complex functions where the extended nested if statements have logic sparsely littered throughout, refactoring or reformatting your code is much more difficult. And adding/extending functionality is even more difficult.
If your nesting if statements are only one or two deep and the logic behind them are easy to understand, then by all means, nested ifs are fine. But in the vast majority of cases (especially cases like your example), using early returns are much easier to write, read, and reason about than nested ifs.
whitet_blacksp@reddit (OP)
the problem i have with the principles of your first example is that it allows relavent mutations in a section that's oftentimes meant to be irrelevant. and since it's in an irrelavent section, it's unlikely to be considered when the procedure is erroneous.
in other words, if there's a bug, it's unlikely that someone debugging is going to examine that "error-handling" section.
the second example is buggy. but the idea of
load_file_into_buffer
taking in a file handle and size is nice.gredr@reddit
So, what you're saying is that aherence to rules without understanding why or when they should be applied is bad, then?
whitet_blacksp@reddit (OP)
i'm confused about what you're asking here.
i don't know where "strict" came from, or why you're asking for a "strict"-adhered example. in the post, i never talked about strictness, so i'm confused.
also, what do you mean by "some" rule? i feel like, if i could present an example of any rule, that would be irrelevant to this post. and what claim am i supposed to counter?
i understand the following tho:
> Strict aherence to rules without understanding why or when they should be applied is bad.
Empanatacion@reddit
Jesus Christ. I just had HRESULT PTSD.
I'm imagining the Holy War the PR turns into.
BlueGoliath@reddit
I stopped reading 25% through. Just no.
thomasmoors@reddit
Is this a joke on early returns on purpose or just awesome coincidence?
BlueGoliath@reddit
Uh yeah... Intentional... totally....
Crafty_Independence@reddit
Did an early return I see.