Loop-switch sequence
Appearance
A loop-switch sequence is a specific derivative of the spaghetti code programming antipattern which describes the obfuscating of a clear process of steps with a byzantine switch-within-a-loop idiom.
Note: it is not an antipattern to use a switch statement within a loop. It is only an antipattern to do so to model a sequence of events. The most common example of a correct use of a switch within a loop is an event handler. (see Event-Driven Programming, Event Loop, and Event-driven finite state machine).
Example of antipattern (pseudocode)
// parse a key, a value, then three parameters
String key;
String value;
List<String> args;
for ( int i = 0; i < 5; ++i )
{
switch( i )
{
case 0 :
key = stream.parse();
break;
case 1 :
value = stream.parse();
break;
default:
args.add( stream.parse() );
}
}
Refactored solution (pseudocode)
// parse a key and value
String key = stream.parse();
String value = stream.parse();
// parse 3 parameters
List<String> args;
for ( int i = 0; i < 3; ++i )
{
args.add( stream.parse() );
}