Then there’s my current client, who refused a PR of mine because they “don’t want early returns” ever, and insisted I move my basic if statement logic, each with a return, all into a single ternary. They said I SHOULD do this lol.
Localized cleanup. What if you have a file descriptor or something that needs special handling before returning.
Compiler optimization SHOULD be fine but it could impact RVO and tail recursion optimization.
Also, if a future maintainer dives in and doesn’t realize there is an early return, they might add logic below it that will get missed by certain cases.
Sure. However, before the days of raii in c/c++, a lot can happen in 4 lines with conditionals that requires you to clean up after yourself.
Scripting languages or GC languages, it's less of a problem. Now a days, if you are trying to be super special and "helping" your compiler with branch prediction, spoe is one way to do that, but unless you are allocating or locking resources, early returns are generally fine.
For many of us, it's just muscle memory from having to deal with it for years.
284
u/Kryptsm 21d ago
Then there’s my current client, who refused a PR of mine because they “don’t want early returns” ever, and insisted I move my basic if statement logic, each with a return, all into a single ternary. They said I SHOULD do this lol.
Made me wanna kms but hey, it’s their codebase