There must be a name for this…
Of course, it would have to be random geekery which brings this blog out of hibernation. Some things just don’t fit in a facebook status.
I spent a bit of time today digging through the FreeRADIUS source code and stumbled across the following new-to-me C idiom:
switch(n) { case FOO: if (condition) { /* stuff */ } else { case BAR: /* more stuff */ } }
The switch statement in C is notoriously powerful and prone to such abuse — see Duff’s device for a particularly infamous example — and I almost filed this in that category.
On further thought, though, this is actually painfully elegant. The effect of the statement is roughly “if case FOO and condition, do stuff, otherwise handle just like case BAR”, but it doesn’t require you to repeat the code for BAR like:
switch(n) { case FOO: if (condition) { /* stuff */ } else { /* more stuff */ } break; case BAR: /* more stuff, again */ }
or cheekily fallthrough as in:
switch(n) { case FOO: if (condition) { /* stuff */ break; } case BAR: /* more stuff */ }
The interleaved statements are weird enough to convey what’s going on in a way that a simple /* FALLTHROUGH */ comment does not.
Duff said of his eponymous device “This code forms some sort of argument in [the fallthrough] debate, but I’m not sure whether it’s for or against.” Duff’s device was good for only a performance improvement, but this one improves clarity, almost. I think that’s a “for”. I’m going to have to use this one.