3 ms·
To me,that depends on the language. If you are using C, it's probably best to have one exit point where everything that needs to be cleaned up is taken care of.
by muststopmyths 7y ago
To me,that depends on the language. If you are using C, it's probably best to have one exit point where everything that needs to be cleaned up is taken care of.
If it's C++ (RAII/scoped lifetimes) or another language that does not require manual cleanup, maybe it's fine to return early.
I personally never use this pattern because I like having a central place to cleanup/set status/return values etc. I also find it easier to add/remove more functionality that way.
The code that I have most often seen using early returns is in large functions that do several things, so they're exacerbating poor design with confusing flow of control.
I feel that if you keep your functions small enough, you can keep them readable either way. So I'm back to mostly worrying about cleaning up resources in a consistent way.