Hacker News new | ask | show | jobs
by predakanga 2077 days ago
There's a particularly famous instance of this, called Duff's Device[0], with a great quote attached (regarding fall-through in case blocks):

> "This code forms some sort of argument in that debate, but I'm not sure whether it's for or against."

[0]: https://en.wikipedia.org/wiki/Duff%27s_device

2 comments

The very article this thread is about talks about Duff's device. This comment confirms my suspicion that most HN readers, like me, read the comments before optionally reading the article.
Often the comments are better than the article :( But yes, you need to be careful in order to not bring up things that were already mentioned. Often it is good enough to read the comments, skim the article, and then only post a comment.
A fair judgement - I had only skimmed the article before replying.

I mostly wanted to provide the quote from Duff, it seemed relevant to the OP.

It's also used to portably implement generators and coroutines. See https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html More generally, the semantics are useful for machine code generation and translation.

Computed goto's are even more useful for the above, but they're an extension. I'd love to see computed goto's added to the C standard, but it's far too late to change the semantics of switch. Rather, just accept that their code flow semantics make them slightly more type safe syntactic sugar for goto--not just in how they're implemented, but in how they can be used.