Long time readers may remember a post from a few decades ago (in Internet time) entitled The FOR-CASE Paradigm. Since originally posting that, I've received quite a number of similar submissions but have avoided using them simply because it would feel like a duplicate post. There's really only so much you can say about a for-switch loop.
Wait, let me repeat the key part of that last paragraph: I've received quite a number of practically identical submissions. That's right. This is just not an isolated incident of WTF but is actually a standard coding practice for quite a few. Following are two of the most recent submissions (both from the past few weeks).
Exhibit A, discovered by Gary Henson, from production code written by a major international company for a government department:
String[] days = new String[7]; for( int i = 0; i < 7; i++ ) { switch(i) { default: case 0: days[i] = "Monday"; break; case 1: days[i] = "Tuesday"; break; case 2: days[i] = "Wednesday"; break; case 3: days[i] = "Thursday"; break; case 4: days[i] = "Friday"; break; case 5: days[i] = "Saturday"; break; case 6: days[i] = "Sunday"; break; } }
Exhibit B, discovered by John, a several-hundred line for-switch loop:
const int nLimits = 256; for( int i = 0; i < nLimits; ++i ) { switch( i ) { case 0: ProcessInternalFindings(); break; case 1: break; case 2: BuildReferences(8,nRef); break; //ED: Snip 2-254 case 255: CleanAllocations(); break; } }