News for hackers. Stuff that matters.
Re:‘Abusing’ the C switch statement – beauty is in the eye o... by ue_ on Thursday February 02, 2017 @11:53AM (#13551993)
>To be clear, it isn't that I find the code difficult to grok: if you don't understand how switch works, please stop using C.

As an aspiring C programmer, is there anywhere I can read about interesting applications of the switch statement? Pretty much everything I've learned has come with neat little break;s and default at the end of each case.

Re:‘Abusing’ the C switch statement – beauty is in the eye o... by lentil_soup on Thursday February 02, 2017 @12:15PM (#13552216)
If you're an aspiring C programmer don't worry about these weird quirks. Don't believe that you need to know that to use C. A switch statement with "neat little breaks and default" is more than what you'll need 99% of the time.
Re:‘Abusing’ the C switch statement – beauty is in the eye o... by iainmerrick on Thursday February 02, 2017 @12:44PM (#13552575)
The remaining 1% is a big comment reading "/* FALL THROUGH */" instead of a break. Once you know what break does, you should know what happens when you leave it out (and why you might want to).

The remaining 0% is stuff like Duff's Device.

Re:‘Abusing’ the C switch statement – beauty is in the eye o... by geofft on Thursday February 02, 2017 @12:40PM (#13552528)
A switch statement is just a bunch of gotos who have hurriedly put on an ill-fitting suit and tie to look respectable.

Any context where you would want "if x == this, goto here; if x == that, goto there" is a potential place to use the switch statement. Duff's device is a good example, but there are many things that can be built this way.

Re:‘Abusing’ the C switch statement – beauty is in the eye o... by dllthomas on Thursday February 02, 2017 @11:06PM (#13557588)
> Any context where you would want "if x == this, goto here; if x == that, goto there" is a potential place to use the switch statement.

Only if "here" and "there" are later on in the same block.

Re:‘Abusing’ the C switch statement – beauty is in the eye o... by pkkim on Thursday February 02, 2017 @11:56AM (#13552022)
Probably not for production use (as most "interesting" code isn't): https://en.m.wikipedia.org/wiki/Duff's_device
Re:‘Abusing’ the C switch statement – beauty is in the eye o... by ConceptJunkie on Thursday February 02, 2017 @05:32PM (#13555521)
You say that, but I pulled out several instances of Duff's Device from production code a few years ago, and replaced them with memcpy( ) and memcmp( ) as appropriate. This isn't 1986 any more.
Re:‘Abusing’ the C switch statement – beauty is in the eye o... by paulrpotts on Friday February 03, 2017 @10:28AM (#13560399)
If you want to read about horrible but entertaining C hacks, check out the entries in the International Obfuscated C Code Contest, the book that came out of the contests a few years back, or books like Deep C Secrets.

http://www.ioccc.org/

As an developer who has written a lot of shipping code in C and C++ for almost 30 years, I would strongly advise you to write your switch statements in as straightforward a way as possible. If you use fall through, place very noticeable /* FALL THROUGH */ comments to indicate that you meant to do that. If you wind up doing some kind of oddball thing because you _have confirmed_ that it results in significantly better optimization for a specific hardware target and compiler, comment the crap out of that, maybe make it conditional with an #if block, and (what I prefer to do is) write the simple version too, test both versions, and keep the simple version as a comment, so a future maintainer can verify that the complicated code is doing what you intended.