this post was submitted on 09 May 2024
459 points (92.4% liked)
Programmer Humor
32588 readers
932 users here now
Post funny things about programming here! (Or just rant about your favourite programming language.)
Rules:
- Posts must be relevant to programming, programmers, or computer science.
- No NSFW content.
- Jokes must be in good taste. No hate speech, bigotry, etc.
founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
I'm sure you are capable of rewriting that in 3 lines and a single nested scope followed by a single return. In fact in languages that use exceptions you have to use at least one subscope.
Notice that in my example I didn't even broach the example with error conditions, cause it's trivial to write cleanly either way. Instead I talked about returns inside business logic. You can't unfuck that. ๐
Since my previous example didn't really have return value, I am changing it slightly. So if I'm reading your suggestion of "rewriting that in 3 lines and a single nested scope followed by a single return", I think you mean it like this?
as opposed to this?
Using a retval has the exact problem that you want to avoid: at the point where we do
return retval
, we have no idea howretval
was manipulated, or if it was set multiple times by different branches. It's mutable state inside the function, so any line from when the variable is defined to whenreturn retval
is hit must now be examined to know whyretval
has the value that it has.Not to mention that the business logic then needs to be guarded with some predicate, because we can't early return. And if you need to add another precondition check, you need to add another (but inverted) predicate to the business logic check.
You also mentioned resource leaks, and I find that a more compelling argument for having only a single return. Readability and understandability (both of which directly correlate to maintainability) are undeniably better with early returns. But if you hit an early return after you have allocated resources, you have a resource leak.
Still, there are better solutions to the resource leak problem than to clobber your functions into an unreadable mess. Here's a couple options I can think of.
defer
Example of option 1
Example of option 2
Example of option 3
Example of option 4
Not sure why you had to do the inverted predicate check again in your first example. You already have the information encoded in the value of retval. It can be written like this:
With a return value you have to add 4 extra lines. This overhead remains constant as you add more checks and more business logic.
Yes all the other suggestions are better than early returns in business logic and would help with leaks. Would be nice if we had RAII outside of C++. I think Rust has it? Haven't done Rust yet.
goto is used in C for this exact kind of early return management. The person you answered to does not maintain code I think
goto cleanup is not the same as return. I didn't badmouth goto cleanup.
This is virtually the same thing with a different keyword, I'd like to hear where you (and the down voters) draw the line.