Sujet : Re: Opinions on `defer`?
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.cDate : 07. Jan 2025, 08:49:33
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vlimad$23nhn$1@dont-email.me>
References : 1
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0
On 07/01/2025 01:35, Alexis wrote:
Hi all,
"Modern C" author Jens Gustedt has been posting on his blog about a
proposed `defer` feature (as provided by e.g. Zig and Go), the most
recent being:
https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/
What do people here think about having such a feature in C?
I've used gcc's "cleanup" attribute a couple of times, typically in connection with somewhat messy stuff hidden behind simple macro names.
I think "defer" could be useful on occasion, and could sometimes give clearer and safer code than "goto"-based error handling. But as with any such feature there is a risk of abuse - too many "defers" in a function will make the code as much a spaghetti monster as multiple goto's. If people use it to keep code neater and reduce the risk of resource leakage, it can be a good thing. If they use it as an excuse to write even bigger and more illegible functions, it's bad.