15 ms·
‘Abusing’ the C switch statement – beauty is in the eye of the beholder
- pdpi 10y agoIf this sort of thing tickles your fancy, you might also want to read about Duff's Device. https://en.m.wikipedia.org/wiki/Duff's_device https://en.m.wikipedia.org/wiki/Duff's_device
- buserror 10y agoI use duff's all the time in micro-embedded; best way to have 'coroutines' without all the necessary stack switching interface. It has it's limitations of course, but it's a terrific tool in certain circumstances.
- doppioandante 10y agoCan you make an example?
- arianvanp 10y agohttps://github.com/arianvp/MidiProject/blob/master/MidiProject.ino https://github.com/arianvp/MidiProject/blob/master/MidiProje... I use duffs device to concurrently read and debounce multiple inputs in my old dj midi controller http://dunkels.com/adam/pt/expansion.html http://dunkels.com/adam/pt/expansion.html
- phkahler 10y ago>> I use duff's all the time in micro-embedded; best way to have 'coroutines' without all the necessary stack switching interface. Can you provide an example? We use state machines all the time, but I have not seen a good example of mixing other flow control statement into a switch/case outside of Duff's Device.
- buserror 10y agoI posted a gist [0] sorry it's not really made for public scrutiny, it's an old pet project clock, a video of most of that code is still there: https://plus.google.com/+MichelPollet/posts/ARDMrY4jSxp https://plus.google.com/+MichelPollet/posts/ARDMrY4jSxp That code does timer, radio clock decoding, scrolling, blinking and all that using multiple 'protothreads'. As you can see the main loop sleep() and wait for a timer interrupt (or anything) to fire to wake up, so it's not even running most of the time; only when the 'tick' timer fires. [0] https://gist.github.com/buserror/9407adb6d52153e16caad5e8a0844023 https://gist.github.com/buserror/9407adb6d52153e16caad5e8a08...
- pcmonk 10y agoCan you point to the usage of Duff's device there? I'm not really seeing it.
- iainmerrick 10y agoFollowing footnotes on Wikipedia led me to this nice clear explanation: http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
- mzs 10y agoThank you, when I saw this blog post it reminded me that years ago I had seen something similar but even more brain melting - it was PuTTY!
- buserror 10y agoThe trick is on the PT_ macros really, they hide the switch, case and gotos. I'll post the header later, but you probably can find it by googling for protothreads
- pcmonk 10y agoAh, I see it now. Thanks!
- merraksh 10y agoGood point. I'm not sure why all snippets have *to = *from++; while it should be *to++ = *from++;
- pdpi 10y agoActually, the examples are correct. The context for this particular snippet is memory-mapped IO, so the `to` pointer is actually the memory address where some external device is mapped to and accepting input. Repeatedly writing to that location causes the values to be sent to the device.
- merraksh 10y agoOK, thanks for the explanation.
- cytzol 10y agoThis is a perfect example of why comments that explain why something is going on, rather than just what is going on, are so useful!
- aninhumer 10y agoI think in this case you could just put the write into a function/macro that documents it's intent.
- to3m 10y agoIt already is - the function is called "send", and rather than wasting its time just doing one write, it does a whole pile of them at once ;)
- mannykannot 10y agoThere are many good reasons for making functions, and occasionally even a good reason to write a macro, but avoiding writing a comment is not one of them.
- otikik 10y agoIt tickles something inside me, but it definitively is not my fancy. And "tickle" is not the right word to describe what it does to me, either.
- mcintyre1994 10y agoOf all the things I learned in kindergarten this was my favourite :) In all seriousness, this is.. interesting! I had no idea you could have a case inside default like that.
- pdpi 10y agoIt's not "inside" the default, as such. I'm not a C expert but, near as I can tell, the labels for a switch statement don't need to occur all be at the same level within the control flow inside the switch body. See my other post here about Duff's Device as another neat example of exploiting this.
- to3m 10y agoBut there is no "inside": case and default are like (slightly restricted) goto labels. They mark a place in the code, nothing more, and, as with goto labels, they can be inside nested scopes. That's why you need the "break" to explicitly leave the switch - because when execution reaches a label, it just continues. The label is a marker, nothing more. (In terms of restrictions: case/default have to be inside the switch statement somewhere, and not inside some additional switch statement nested within it. But aside from that, as demonstrated by the article, you have a good deal of freedom about where they go.)
- mcintyre1994 10y agoThis was a really helpful explanation, thankyou :)
- DannyB2 10y agoNo need to abuse the C switch statement. #define while if // make code faster #define struct union // use less memory
- elmigranto 10y agoJust define all the keywords to be `exit(0);` #performance :)
- geofft 10y agoI once got in trouble in high school for encouraging a classmate who'd misread an assignment to do `#define int double` to fix it.
- rootbear 10y agoBut don't forget that defining define is undefined!
- neoeldex 10y agoHe's got a good point: Why is C still the staple it is? But I believe that Rust is going to take it's place. Not sure whether it's a good thing, but this trick isn't possible in rust ;) Rust would give an error when an enum isn't exhausted in a match clause. So the issue being described doesn't exist :D
- pdpi 10y agoIt'll be a while still before Rust makes its way to all the platforms with a C compiler (if it ever even happens). As to why it's still a popular language... The C99 spec document is shorter than the Ecma-262 (Javascript) spec by around 50 pages. The parts dedicated to the _languages_ themselves (as opposed to the builtins/standard libraries) weigh in at 130 pages for C, or around 300 pages for JavaScript. C, despite its warts, is a small, simple language.
- catnaroek 10y agoHow much longer would the C specification be if it were fully formalized? And even then, a formalization isn't enough: it has to be useful for actually proving theorems, e.g. “such and such pattern is portable across conforming implementations”, and the size of the proof better not be much larger than the size of the program itself. If the C standard is so useful, try programming using the C standard as your sole debugging tool.
- drivers99 10y ago> Rust would give an error when an enum isn't exhausted in a match clause. So the issue being described doesn't exist :D I was going to say something like that, but the equivalent code would have a "_" case in the match statement. I don't know rust yet, but would a rust programmer typically make a point to not use a "_" (default) statement so that you can catch these at compile time and expect you'll never have anything else?
- geofft 10y agoIt's not just typical, it's required. If you add a default match when you've already specified all the possible cases, the compiler gives you an error. Here's an example (https://is.gd/KjSc8B https://is.gd/KjSc8B): enum Foo { Bar(i32), Baz(i32), } fn matchfoo(f: Foo) -> i32 { match f { Foo::Bar(x) => x, Foo::Baz(x) => x, _ => 42 } } gives "unreachable pattern", a hard error, for the "_" case.
- qwertyuiop924 10y agoI didn't learn this in kindergarten, but I did learn about Duff's Device in the 7th grade (as it turns out, the Jargon File makes for pretty good reading during boring classes). Never had cause to implement it, though, and never saw this particular hack.
- greyman 10y agoFor me, the beauty is when it is apparent on the first sight what the code does. This is just the opposite of beauty (in my eyes :-)).
- marcosdumay 10y agoThere is more to code than legibility. In general, the priorities are correctness, legibility, legibility, a few hundred more times legibility, then stuff like performance, memory usage, binary size, predictability, etc. But this is C. C is used on a wide diversity of contexts, and on some of then the order above changes.
- gpderetta 10y ago... except when performance, memory usage, binary size, predictability are part of the correctness requirement (which is most of the time in the areas C is used).
- Sanddancer 10y agoWhen dealing with embedded stuff, you don't always have the luxury of code at first sight. Sometimes you have to write ugly code like this code does, sometimes you have to roll your own print statements because using printf pulls in a few k of code, sometimes you have to use a uart running at 600 baud to get a 60 hz timer. Of course, when you start going down the path of interesting, it's always best to write lots of good documentation to go with the cleverness, so people understand what's going on.
- peapicker 10y agoMy favorite abuse of switch statements in C is to use them for co-routines in C... http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html http://blog.robertelder.org/switch-statements-statement-expressions/ http://blog.robertelder.org/switch-statements-statement-expr...
- huntergoldstein 10y agoTo add onto this: - http://dunkels.com/adam/pt/ http://dunkels.com/adam/pt/ Even though it is an abuse, these sorts of libraries are fascinating to me: making use of language features in unexpected (most likely unintended) ways.
- peapicker 10y agoAgreed! (I had forgotten about protothreads, thanks)
- delinka 10y agoDid I miss an explanation somewhere? Why are the `process_` functions referenced again in the default case? That has a code smell.
- pdpi 10y agoDid you reach the end of the post? In the final version of the code, each of those functions is used exactly once.
- delinka 10y agoI did. But not requiring the `if` at all obviates the need for this `switch` abuse. Hence my question about its necessity.
- pdpi 10y agothe ifs allow the code to function correctly even if you forget to update this function with newly-added cases in the enums. The switch statement makes the known-cases behave O(1), while also bypassing unneeded validation.
- akkartik 10y agoThe known cases are O(1) even with an `if` -- all it's doing is a range check. Any performance benefit from this hack is miniscule at best. It may even end up confusing a compiler and making performance worse.
- pdpi 10y agoSure. I'm not arguing it's a good idea. It just serves as (somewhat forced) motivation for showing how to exploit this behaviour with switch labels, which is the real point of the post.
- drivers99 10y agoIf the number of options (CMD5, CMD6, etc) changes, he needs the code to not break if he adds them to the enum but doesn't doesn't extend the switch statement.
- YSFEJ4SWJUVU6 10y agoNeat `abuse'. Personally I don't like the what I consider an anti-pattern he used in his refactored functions. I'm talking about doing this: if (condition) return true; return false; instead of simply return condition;
- catnaroek 10y agoThat's not even an anti-pattern. It's just being ignorant of the basic properties of the Boolean type.
- unwind 10y agoYeah, I hate that with a burning passion, too. The only justification I've seen/heard is that it makes it more "debuggable", you can single-step through and see that the expected path is taken, i.e. that the condition is properly evaluated. Still, I hate it and would much rather check that some other way (by inspecting the return value before the function exits, for instance).
- pdpi 10y agoIf you're going for debuggable, though, you still don't want the if and multiple returns. You'd be better off with something like: bool result = (condition); return result;
- chillingeffect 10y agoyes, I wish everyone did this all the time. even to the point of: bool result = (condition); if( result ) { do_some_shit(); } else { do_some_different_shit(); } p.s. how did you format the code section of your post?
- pdpi 10y agoPrefix with 4 spaces. :)
- adekok 10y agoThere's just no reason to have a web site that goes down when HN viewers go to it. I run wiki.luajit.org on a VM, using Gollum. We got hit by HN a while back. CPU went from 1% to 2%. People saying "CPU is commodity" tend to write software which uses all available CPU... for little purpose. Heck, I worked at a company which did just that. "CPU / memory / disk is commodity, use it." And they did! Soon enough, all CPU / memory / disk was in use, and they had to go back and re-architect their software so that it wasn't crap. It would have been cheaper to do it right in the first place. But religious beliefs about engineering over-rode actual engineering.
- anjc 10y agoCool trick, but difficult to grasp on first glace. I'd personally rather see pages of 'if' statements if it makes it easier to reason about.
- noobermin 10y agoSo the main point here is we save some time with a direct jump at least for a few values of x? It's cool I guess but the extra confusion might not be worth it.
- Sanddancer 10y agoThe author is coming from the embedded world, where saving time in an important loop can determine whether or not your code can operate in the required time windows. Keep in mind that there are a lot of micros out there that are being used for new projects that have memory amounts in the hundreds or even tens of bytes. When you start getting into those areas, sometimes you have to cheat to get things working.
- noobermin 10y agoSure, in that case, then I agree with the saurik. The solution is you just put in more labels in your switch statement. And, if you aren't so keen on that sort of discipline, use a macro ALL_CMDS so you don't have to change nine places in the code. Go "O(1)" on all the CMDs or don't, for the sake of ease of comprehension. In the hypothetical, or may be practical world[0] where size+computation constraints require you must goto some of them and if-else the rest of them, I suppose the "1/2goto,1/2if-else" optimization would be one of the latter considerations for optimization over a host of other things you can consider. [0] I admit I don't hack in that world, but I do computational physics, which is more about vectorizing things, not optimization of branching typically.
- julian_1 10y agoA compiler like gcc will (or at least used to) translate small switch statements into compare/jmp assembler, and larger switch statements into a jump table using indexes/modulos on the test expression. The tipping point for when the compiler decides to switch strategies, is obviously architecture dependent but could also be influenced by what flags are set and whether to favor for small code side, or highest speed. I suspect, the code could have been written using two switch statements - and probably achieved the same effect in terms of the generated assembler, as well as be more idiomatic.
- saurik 10y agoI think this looks a lot better if you use "if (false);" at the top to normalize the "else if" in the rest of the chain. switch (x) default: if (false); else if (valid_command_message(x)) case CMD1: case CMD2: case CMD3: case CMD4: process_command_msg(x); else if (valid_status_message(x)) case STATUS1: case STATUS2: case STATUS3: process_status_msg(x); else report_error(x); That said, I think the entire concept of this code having both the enum switch and the valid_* functions is just horrible :/. 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. The issue is that this particular use case for the switch is a lazy performance optimization, as it should just totally replace the valid_* calls if we do that; it is like the author doesn't want to update the switch cases, but wants the code to keep working? Did they just forget the switch exists, or is it that they can't edit it? I just don't get it, particularly when any modern C compiler supports "give me a warning if I don't consume all the possible switch values in this enum", which is the feature that this code should be using, not if < END_*. BTW: it isn't clear to me that this would even be a performance optimization; in an ironic twist, many compilers are going to choose to compile that switch statement into the moral equivalent of two if statements with range checks, as default has to be implemented as a range check anyway, and if you work out how many range checks you are doing combined with the code cache benefits of having explicit branches instead of implicit jump tables, this switch statement is going to feel extra repetitive when you look at the machine code.
- ue_ 10y ago>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.
- pkkim 10y agoProbably not for production use (as most "interesting" code isn't): https://en.m.wikipedia.org/wiki/Duff's_device https://en.m.wikipedia.org/wiki/Duff's_device
- janekm 10y agoThis is just terrible. The fact that the compiler complains if an enum is added without updating the switch statement is a feature. The enum and switch version is clear and explicit. To defeat it, in the name of... what, code obfuscation? beggars belief.
- lisper 10y agoOf course if you were programming in a language with proper macros you could just automatically generate the switch statement from the enums and have the best of all possible worlds.
- technofiend 10y agoPeople may disagree but if the function valid_command_message already bounds checks x, isn't the case statement redundant? Do you really want the same bounds check in two different places? Maybe in embedded programming you do for safety but it seems like a simple if test is sufficient.
- ifdefdebug 10y agoThe switch jumps directly to the case labels, skipping the valid_command_message call. At least that's the concept, but you better know what your compiler makes out of it. So this might be useful if you want the jump table performance now on the known values, but you suspect that in the future some maintainer might add more values to the enums and forget about the switch, because maybe the enums are hidden in some header file far away.
- LeifCarrotson 10y ago> If the implementation uses a form of jump-table then this [switch statement] has the benefit of giving you an O(1) performance based on the messages, whereas with the if-else-if chain, the commands will be checked and processed before status and errors. The reason for using a switch statement in the first place was so that you'd have constant performance. The argument against the simple implementation was: > Let’s assume in v2.0 of the system we want to extend the message set to include two new commands and one new status message ... the if-else-if version will handle the change without modification, whereas the existing switch statement treats the new commands still as errors. Treating the new commands as errors is a feature of the switch statement style, not a problem. They need to be added to the jump table, not implemented with reduced performance.
- derriz 10y agoAlso using big O notation here makes no sense surely? The claim that the switch version is (in contrast) O(1) is not even wrong.
- clusmore 10y agoYeah I'm not quite sure I understand this statement either. Does he mean that in the case of a command the first `if` passes and the rest are ignored, but in the case of status and error you need to perform the previous `if`s as well? So then the case for status is what, O(2)? That makes no sense. If you're going to write something this obscure for the sake of "performance", you want to be damn sure it's worth it -- that performance is even an issue, and that this is a large enough improvement to justify not doing the simple thing. Honestly it feels like the author wants to do it this way because it's clever, and the "performance" reason is just rationalising it.
- iainmerrick 10y agoI guess the reminder that C's switch syntax is bizarre and flexible is amusing, but this is still really wrong-headed. Friends don't let friends write code like this! We're told we have "process_command_msg" and "process_status_msg" functions. Each of those functions is already doing a switch or if/else to determine the exact message type. If you care about the cost of those lookups, the correct thing to do is have a single switch statement that handles all the messages at once. If you don't care enough about the cost of lookups to split up those functions, you should stick with the obvious if/else test. Another thing you could do to improve the design is to have a "get_message_type" function returning a "COMMAND_TYPE" or "STATUS_TYPE" enum, then switch on that. That can be made efficient if you really care about execution cost (for example, check a single bit, and inline it so the switch can be optimized). Abusing the switch statement not only gives you unreadable and bug-prone code, it simply isn't any better than a sensible approach.
- lend000 10y agoInteresting ideas, but this bothers me: "Then refactored to:" Refactored implies performance improvement to me, and inlining is almost always faster than the more modular representation that the author ended up with after "refactoring" (putting the comparisons in functions).
- kiliancs 10y agoReadability improvements and complexity reductions are often more important than performance improvements and very good reasons to refactor.
- JoachimSchipper 10y agoWhere available, my preferred solution is to compile with gcc -Wall -Werror (or equivalent); this will enable -Wswitch, causing a compile error when you fail to handle an 'enum' case. Needing to fix a few obvious compile errors after a change isn't much of a burden; un-maintainable code is code that silently breaks on changes. (The same attitude leads to lot of static_assert().) (You may also be interested in -Wswitch-enum.)
- nwmcsween 10y agoNo do not do -Wall and -Werror as Wall is arbitrary between compilers and versions.
- JoachimSchipper 10y agoYou're absolutely right that -Werror should not be the default in release tarballs, and I should have included that.
- GFK_of_xmaspast 10y agoThis is a whole stack of horrible code under the banner of "might be more efficient" with no actual examination of what the code actually gets compiled into, much less any profiling.
- posedge 10y agowell, I dont see the beauty :)
- posedge 10y agowell, I dont see the beauty :)
- rootbear 10y agoWhile we're discussing abuse of switch, here's my personal best effort at misusing this feature, from over thirty years ago. /* * A program to print The Twelve Days of Christmas * using the C fall through case statement. * * Jim Williams, jim@maryland, 2 May 1986 (but first written ca. 1981) */ /* * If you have an ANSI compatible terminal then * #define ANSITTY. It makes the five Golden rings * especially tacky. */ #include <stdio.h> char *day_name[] = { "", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth" }; int main() { int day; printf("The Twelve Days of Christmas.\n\n"); for (day = 1; day <= 12; day++) { printf("On the %s day of Christmas, my true love gave to me\n", day_name[day]); switch (day) { case 12: printf("\tTwelve drummers drumming,\n"); case 11: printf("\tEleven lords a leaping,\n"); case 10: printf("\tTen ladies dancing,\n"); case 9: printf("\tNine pipers piping,\n"); case 8: printf("\tEight maids a milking,\n"); case 7: printf("\tSeven swans a swimming,\n"); case 6: printf("\tSix geese a laying,\n"); case 5: #ifdef ANSITTY printf("\tFive ^[[1;5;7mGolden^[[0m rings,\n"); #else printf("\tFive Golden rings,\n"); #endif case 4: printf("\tFour calling birds,\n"); case 3: printf("\tThree French hens,\n"); case 2: printf("\tTwo turtle doves, and\n"); case 1: printf("\tA partridge in a pear tree.\n\n"); } } return 0; }
- seishun 10y agoThis is hardly abuse. This is exactly how fallthrough was intended to be used.
- Tarean 10y agoI mean, an array could have worked but I think this is more readable than a clever version would have been. main = mapM_ putStrLn $ reverse verses where verses = combine <$> enumeratedSlices songLines combine (i, s) = "On the %s day of Christmas, my true love gave to me\n%s" % dayNames !! i $ concat s enumeratedSlices = zip [0..] . init . tails
- vram22 10y agoPartly related: Simulating the C switch statement in Python: https://jugad2.blogspot.in/2016/12/simulating-c-switch-statement-in-python.html https://jugad2.blogspot.in/2016/12/simulating-c-switch-state... Not a proper simulation of C's switch, has limitations mentioned in the post, just something I whipped up for fun.
- chiph 10y agoAbuse of the switch statement? The firm bought a company and we found in their source code that they had created a 50,000 case-statement monster. We asked that they refactor it, and they agreed because the sheer size of it was giving the compiler problems. So they divided it into two separate 25,000 case-statement monsters.
- noobermin 10y agoThat moment when your jump causes a cache miss.
- kayamon 10y agoMany compilers now support a range selection extension on case labels, so you can just do: case CMD .. CMD_END: /* whatever */ break;
- peter303 10y agoModal behavior should implemented through subclassing, not switches or if tests. This has the property of consolidating all code for a class in the same code file and not scattered among larger pieces of code. Much easier to maintain and extend.
- kwhitefoot 10y agoWhat classes? This is C not C++ or C#. I don't dispute that one can use C for class based coding but it isn't common.
- DannyBee 10y agoMeh, this is pointless, the compiler chooses if ranges or jump table regardless of which form you use here. But it probably makes you feel better :)
- gp7 10y ago1) All four versions are O(1) 2) Textually interleaving two implementations is bad no matter what language you're in 3) You could achieve the same effect with gotos without giving anyone a migraine... but it still wouldn't be worth it
- gorb314 10y agoI'm the author of libb64[1], which (ab)uses the switch statement in a similar way to implement a base64 encoder and decoder. This was inspired in turn by "Coroutines in C"[2], written by Simon Tatham. [1] http://libb64.sourceforge.net http://libb64.sourceforge.net [2] http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
- deleted 10y ago[deleted]
- jadengeller 10y agoMacro lists are a rather elegant solution IMO. You cannot add cases to the enum without also adding cases to the switch. #define CMD_LIST CMD(1) CMD(2) CMD(3) CMD(4) #define STATUS_LIST STATUS(1) STATUS(2) STATUS(3) enum { #define CMD(x) CMD ## x = x - 1, CMD_LIST #undef CMD END_CMD }; enum { #define STATUS(x) STATUS ## x = x + 9, STATUS_LIST #undef STATUS END_STATUS }; void process_message(int x) { switch (x) { #define CMD(x) case CMD ## x: CMD_LIST #undef CMD process_command_msg(x); break; #define STATUS(x) case STATUS ## x: STATUS_LIST #undef STATUS process_status_msg(x); break; default: report_error(x); break; } } It's unfortunate that it isn't possible to write a macro macro that expands to that gross def/undef boilerplate we use in the switch.
- evincarofautumn 10y agoYou can use an x-macro: // cmds.def X(1) X(2) X(3) X(4) #undef X // elsewhere.h enum { #define X(x) CMD##x = x - 1, #include "cmds.def" END_CMD }; Or a higher-order macro: #define CMD_LIST(X) X(1) X(2) X(3) X(4) enum { #define CMD(x) CMD##x = x - 1, CMD_LIST(CMD) #undef CMD END_CMD };
- nialv7 10y agoIt always amaze me how many people is 'discovering' the C switch statement for the first time.
- MichaelMoser123 10y agoWhile dealing with unhandled enumeration constants you can get help from most compilers; for gcc: #include <stdio.h> typedef enum tagCmd { cmd1, cmd2, cmd3 } Cmd; void doCmd(Cmd cmd ) { switch(cmd) { case cmd1: printf("Cmd1\n"); break; case cmd2: printf("Cmd2\n"); break; //case cmd3: // printf("Cmd3\n"); // break; } } int main() { doCmd(cmd1); return 0; } Now gcc -Wall t.c t.c: In function ‘doCmd’: t.c:7:2: warning: enumeration value ‘cmd3’ not handled in switch [-Wswitch] switch(cmd) ^ Now gcc -Wall -Werror t.c t.c: In function ‘doCmd’: t.c:7:2: error: enumeration value ‘cmd3’ not handled in switch [-Werror=switch] switch(cmd) ^ Or rather the following: (if you don't want to deal with a lot of other errors/warnings) gcc -Wswitch -Werror t.c t.c: In function ‘doCmd’: t.c:7:2: error: enumeration value ‘cmd3’ not handled in switch [-Werror=switch] switch(cmd) ^ If you put in a default statement then the compiler will not be able to help you here.
- lsiebert 10y agoI think you can probably get by with an array of function pointers, and bittwiddling to get values. For example, if i is the ENUM code that was passed, then 1 ^ ((unsigned int)i >> (sizeof(int) * sizeof(char) - 1)) will be 0 if negative and 1 if positive or zero.
- jmts 10y agoThis is ridiculous. Sure, I'll agree, it's a neat trick, but what mindset do you need to be in to come up with such a convoluted solution to such a problem, and the wrong problem at that? What problem does hack this solve? Single point of edit for a new feature? Well, odds are you've added/edited code elsewhere to implement the feature. Saving one edit is pointless. Just do it properly. What problem is this code trying to solve? Message dispatch. And it does it. Twice. Once to differentiate whether something is a command or a status message, and then once again to actually execute the thing. Silly. Just build a table and be done with it. If you're concerned about performance after you've benchmarked it, write some code to generate a perfect hash and generate your table. static const struct message_handler { int id; void (*process)(int x); } tab[] = { { .id = CMD1, .process = process_cmd1, }, { .id = CMD2, .process = process_cmd2, }, { .id = CMD3, .process = process_cmd3, }, // ... { .id = STATUS1, .process = process_status1, }, { .id = STATUS2, .process = process_status2, }, { .id = STATUS3, .process = process_status3, }, }; const struct message_handler* find_message_handler(int x); void process_message(int x) { const struct message_handler *handler; handler = find_message_handler(x); if (NULL != handler) { handler->process(x); } else { report_error(x); } } Now tell me which one your code reviewers and juniors are going to prefer.
- zik 10y ago> What problem is this code trying to solve? I think you missed the point of this hack - the problem he's trying to solve is described in the article: > The common issue associated with switch statement is typically maintenance; especially where the set of ‘valid’ values needs extending. He wants the efficiency of the switch statement for all the cases he knows about and a fallback to the less efficient if clauses if that fails due to new cases being added. It's a pretty silly example but let's face it - it's more for fun than elegance.
- jmts 10y agoI see your point, but I'd argue that even then it's still the wrong the problem. 1. If you don't enjoy maintaining your switch statement, it's probably huge anyway and you need to refactor. 2. If you don't enjoy maintaining this switch statement, you probably also don't enjoy maintaining the switch statement inside next function that gets called that gives you your full set of valid values. 3. The number of types of messages used is probably much fewer than unique messages, so you are optimising on a small N, which will give minimal returns. 4. You never need to look at this code again and it will just sit there adding overhead. Other switch statements will be maintained because they're the first place you'll notice missing entries. Fun, sure. Like setting a fistful of matches on fire. It takes a bit of effort to set up, and it looks interesting. I'll agree to that.
- detrino 10y agoIt's impressive that clang generates the same assembly for both versions: https://godbolt.org/g/yLh1EF https://godbolt.org/g/yLh1EF Note that the assembly for process_message2 calls each function only once even though some appear twice in the body.
- loup-vaillant 10y ago"Fixing" the switch statement. #define case break; case #define default break; default There, no more fall through.