3 ms·
> The worst offender is the quasi-obligatory "break" statement in "case" blocks. This was (almost certainly) done to simplify the compiler. CASE in C is not a
by lisper 3y ago
> The worst offender is the quasi-obligatory "break" statement in "case" blocks.
This was (almost certainly) done to simplify the compiler. CASE in C is not actually a structured control construct despite its syntactic appearance, it's just a computed GOTO. This is what makes things like Duff's device [1] possible.
[1] https://en.wikipedia.org/wiki/Duff%27s_device https://en.wikipedia.org/wiki/Duff%27s_device
- itsphilos 3y agoI didn't know about Duff's device, which looks pretty interesting! Thanks for sharing
- lisper 3y agoSure, but just for the record IMHO duffs device is an abomination and an illustration of how badly broken C is. No one should ever actually use it.
- commandersaki 3y agoI think I've come across Duff's device in Go internals for zero-ing memory.
- codr7 3y agoYeah, I remember having my mind blown when I first discovered Duff's Device. My mental picture of what C-compilers are up to turned out to be a lot more sophisticated than reality. But having written a few Forth-interpreters, it makes total sense to me. Treating the input as a mostly unstructured stream of tokens is very convenient.
- bxparks 3y agoI think computed-GOTO is easier to understand and more flexible than Duff's device, but computed-GOTO is not part of the C standard. It is sometimes available as a non-portable extension, e.g. https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html. Anyone know why it is not included in C?
- lisper 3y agoComputed go to was not quite an accurate description. I probably should have said “dispatch table” instead.
- nuancebydefault 3y agoWhat I find inconvenient is that a case body can be made with or without curly brackets. I'd rather have they were mandatory, like when an if statement aplies to more than one statement. The downside is that you need more indentation to have a logical curly brackets structure. Choices/options for the same result, tend to make code less readable.