Live data from Hacker News

C: Simple Defer, Ready to Use

gustedt.wordpress.com

11–20 of 157 posts

Re: C: Simple Defer, Ready to Use

#11
post #4

Do defers get processed if an exception is thrown? I know C doesn’t have exceptions, but real world C code often interacts with C++ code. So it is necessary imo to define that interaction. In C++ we have something pretty similar already in the form of Folly ScopeGuard (SCOPE_EXIT {}).

I am reminded of this:

https://news.ycombinator.com/item?id=42532979

Interactions with stack unwinding are not considered by C, so I doubt this would be. Compilers would be free to make it work, however. This is just my guess.

Re: C: Simple Defer, Ready to Use

#14

Very nice, using the goto trick to perform cleanups always felt dirty to me.

gotos can be used without shame. Dijkstra was wrong (in this rare case). defer is cleaner, though.

Dijkstra was not wrong. Modern programmers are wrong in thinking that the goto that they use is what Dijkstra was talking about, merely because of the fact it happens to be called the same thing. I mean, I get how that can happen, no sarcasm, but the goto Dijkstra was talking about and what is in a modern language is not the same. https://jerf.org/iri/post/2024/goto/

The goto Dijkstra is talking about is dead. It lives only in assembler. Even BASIC doesn't have it anymore in any modern variant. Modern programmers do not need to live in fear of modern goto because of how awful it was literally 50 years ago.

Re: C: Simple Defer, Ready to Use

#15
Unfortunately, this requires an executable stack, and only works on gcc, though an alternate implementation can work on clang.

After a few attempts at defer, I ended up using a cleanup macro that just takes a function and a value to pass to it: https://github.com/aws-greengrass/aws-greengrass-lite/blob/8...

Since the attribute or a function-like macro in the attribute position broke the c parsing in some tooling, I made the macro look like a statement.

Re: C: Simple Defer, Ready to Use

#16
post #14

Earlier quoted context omitted.

gotos can be used without shame. Dijkstra was wrong (in this rare case). defer is cleaner, though.

Dijkstra was not wrong. Modern programmers are wrong in thinking that the goto that they use is what Dijkstra was talking about, merely because of the fact it happens to be called the same thing. I mean, I get how that can happen, no sarcasm, but the goto Dijkstra was talking about and what is in a modern language is not the same. https://jerf.org/iri/post/2024/goto/ The goto Dijkstra is talking about is dead. It liv…

Or the tl;dr in modern parlance Dijkstra was railing against the evils of setjmp.

Re: C: Simple Defer, Ready to Use

#18
post #14

Earlier quoted context omitted.

gotos can be used without shame. Dijkstra was wrong (in this rare case). defer is cleaner, though.

Dijkstra was not wrong. Modern programmers are wrong in thinking that the goto that they use is what Dijkstra was talking about, merely because of the fact it happens to be called the same thing. I mean, I get how that can happen, no sarcasm, but the goto Dijkstra was talking about and what is in a modern language is not the same. https://jerf.org/iri/post/2024/goto/ The goto Dijkstra is talking about is dead. It liv…

"when I see modern code that uses goto, I actually find that to be a marker that it was probably written by highly skilled programmers. "

He should have said "correct code", not "modern code" because the times I remember seeing goto the code was horribly incorrect and unclear.

(With break and continue, someone has to be doing something extra funky to need goto. And even those were trigger signs to me, as often they were added as Hail Mary's to try to make something work)

{I typically reviewed for clarity, correctness, and consistency. In that order}

Re: C: Simple Defer, Ready to Use

#19
I really, really do not like what that could do to the readability of code if it was used liberally.

It's like GOTOs, but worse, because it's not as visible.

C++'s destructors feel like a better/more explicit way to handle these sorts of problems.

Re: C: Simple Defer, Ready to Use

#20
post #16
post #14

Earlier quoted context omitted.

Dijkstra was not wrong. Modern programmers are wrong in thinking that the goto that they use is what Dijkstra was talking about, merely because of the fact it happens to be called the same thing. I mean, I get how that can happen, no sarcasm, but the goto Dijkstra was talking about and what is in a modern language is not the same. https://jerf.org/iri/post/2024/goto/ The goto Dijkstra is talking about is dead. It liv…

Or the tl;dr in modern parlance Dijkstra was railing against the evils of setjmp.

Lua uses it for error handling. It is really hard to understand the lua code. :/
Post reply on HN