Re:C: Simple Defer, Ready to Use
by accelbred on Monday January 06, 2025 @03:00PM
(#42614936)
Destructors are hidden control flow, and can be non-obvious for structs from other files. I find they make code significantly harder to follow than in plain C. Defer does not have the problem, as all the logic in your function is explicitly there.
[ Parent
| on HN ]
Re:C: Simple Defer, Ready to Use
by quelsolaar on Monday January 06, 2025 @05:15PM
(#42616614)
Not right there, some other place in the function. Also people will start adding defer in macros and then things will go sideways.
Re:C: Simple Defer, Ready to Use
by jefbyokyie on Monday January 06, 2025 @07:07PM
(#42617640)
> Not right there, some other place in the function.
Exactly. Both C++ RAII (constructors/destructors) and C23 defer are awful. They make the behavior implicit; in effect they tie an indefinitely large (and growing) baggage to scope termination without the source code reflecting that baggage.
Cascading gotos (or the arrow pattern) are much better. Cleanup code execution is clearly reflected by location in the source code; the exit path can be easily stepped through in a debugger.