Loop-switch sequence: Difference between revisions

Content deleted Content added
No edit summary
General cleanup
Line 1:
A '''loop-switch sequence''' is(also aknown specificas derivativethe of'''for-case theparadigm'''<ref>[http://thedailywtf.com/Articles/Switched_on_Loops.aspx Switched on Loops] at [[spaghettiThe codeDaily WTF]]</ref>) is a a programming [[antipattern]] where a clear set of steps is implemented as a byzantine switch-within-a-loop. AlsoThe knownloop-switch assequence "Theis FOR-CASEa paradigm"specific derivative of [http://thedailywtf.com/Articles/Switched_on_Loops.aspx[spaghetti code]].
 
'''Note: ''' itIt is not necessarily an [[antipattern]] to use a switch statement within a loop. Itloop—it is only considered incorrect when used to model a known sequence of steps. The most common example of the correct use of a switch within a loop is an event handler. In event handler loops, the sequence of events is not known at compile-time, so the repeated switch is both necessary and correct (see [[Eventevent-driven programming]], [[Eventevent loop]] and [[Eventevent-driven finite state machine]]). It is also reasonable to have a check for the possible performance loss that having a conditional inside a loop could bring. Today modern compilers are capable in some cases to transform the loop with conditional(s) into a construct with better performance (see [[loop unswitching]]); therefore it may not always be worthwhile to eliminate these constructs for performance reasons alone, although it may benefit readability to do so.
 
It is reasonable to check for the possible performance loss that having a conditional inside a loop could bring. Modern compilers are capable in some cases to transform the loop with conditional(s) into a construct with better performance (see [[loop unswitching]]); therefore, it may not always be worthwhile to eliminate these constructs for performance reasons alone, although it may benefit readability to do so.
==Example of antipattern (pseudocode)==
 
==Example==
Here is an example of the antipattern:
 
<source lang="java">
// parse a key, a value, then three parameters
String key;
String value;
List<String> args;
 
for (int i = 0; i < 5; i++) {
// parse a key, a value, then three parameters
switch(i) {
 
case 0 :
String key;
key = stream.parse();
String value;
break;
List<String> args;
case 1 :
for ( int i = 0; i < 5; ++i value = stream.parse();
break;
{
switch( i ) default:
args.add( stream.parse() );
{
case 0 :
key = stream.parse();
break;
case 1 :
value = stream.parse();
break;
default:
args.add( stream.parse() );
}
}
 
</source>
 
And here is the refactored solution:
==Refactored solution (pseudocode)==
 
<source lang="java">
// parse a key and value
String valuekey = stream.parse();
String keyvalue = stream.parse();
String value = stream.parse();
// parse 3 parameters
 
// parse 3 parameters
List<String> args;
for ( int i = 0; i < 3; i++i) ){
value = args.add(stream.parse());
{
}
args.add( stream.parse() );
</source>
}
 
==References==
 
<references />
</source>
 
[[Category:Anti-patterns]]