2 ms·
I've seen peopl go to great lengths to have only one return keyword. Unreadable mess with a "retval" variable declared at the top and then you have to read EVER
by dusted 6y ago
I've seen peopl go to great lengths to have only one return keyword. Unreadable mess with a "retval" variable declared at the top and then you have to read EVERYTHING to identify all the places it could change, and then figure out which of them (there can be multiple) changes it..
A return statement IMO makes it much more clear that "yeah, we're done under this circumstance"
I think it comes from someone mistaking "keep it simple and easy to read" with "ALWAYS THAU MUST DO THIS AND NEVER THAT!"
- zikzak 6y agoIt's too bad goto has been so demeaned. It's great for "we're done here but I always want to do a few things before we exit". Like logging. That's it actually why I always work to a single return. I like to know, right before I exit, that I can clean references up, or write to a log, or whatever and that only has to happen in one place. If I'm patching some legacy code, I won't usually refactor to achieve this, but I might.
- freemium413 6y agoWhen I come across a situation like that, i consider the chunk of code reusable and split it into a separate function. It may not be perfect and you might need to make that function more generic than it was but I think it makes sense.
- bobbylarrybobby 6y agoSome languages support this with try/finally or a `defer` statement that you can place near the top of the function