Re:Avoid Else, Return Early (2013)
by jstimpfle on Monday March 26, 2018 @11:34AM
(#16679632)
Wrong. You need to exit before resource allocations :-). And in general if you allocate resources in most functions you're doing it wrong. Need global resource managers. Stack variables should only very rarely be resource owners.
[ Parent
| on HN ]
RAII works just as well for heap allocated objects as it does for stack allocated ones.
Sure, but allocating resources on the stack is not good for maintainability. Software is easier to understand if its state is not hidden in semi-permanent call stack frames.
In general I agree with you, but that seems orthogonal to the subject at hand.
Parent's statement was that early returns are hard because you need to cleanup allocated resources. I don't think so, because there shouldn't (usually) be resources "on the stack".