Loop-switch sequence: Difference between revisions

Content deleted Content added
No edit summary
 
(13 intermediate revisions by 12 users not shown)
Line 1:
{{Orphan|date=October 2022}}
A '''loop-switch sequence''' (also known as the '''for-case paradigm'''<ref>[http://thedailywtf.com/Articles/The_FOR-CASE_paradigm.aspx The FOR-CASE paradigm] and [http://thedailywtf.com/Articles/Switched_on_Loops.aspx Switched on Loops] at [[The Daily WTF]]</ref> or Anti-[[Duff's Device]]) is a programming [[antipattern]] where a clear set of steps is implemented as a switch-within-a-loop. The loop-switch sequence is a specific derivative of [[spaghetti code]].
 
A '''loop-switch sequence'''<ref name="LEVEL ">{{cite web | url=https://levelupcode.wordpress.com/avoid/loop-switch-sequences/ | title=Loop-switch sequences | date=30 November 2013 | publisher=LEVEL UP CODE | accessdate=11 April 2016}}</ref> (also known as the '''for-case paradigm'''<ref>[http://thedailywtf.com/Articles/The_FOR-CASE_paradigm.aspx The FOR-CASE paradigm] and [http://thedailywtf.com/Articles/Switched_on_Loops.aspx Switched on Loops] at [[The Daily WTF]]</ref> or Anti-[[Duff's Device]]) is a programming [[antipattern]] where a clear set of steps is implemented as a switch-within-a-loop. The loop-switch sequence is a specific derivative of [[spaghetti code]].
It is not necessarily an [[antipattern]] to use a switch statement within a loop—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 [[event-driven programming]], [[event loop]] and [[event-driven finite state machine]]).
 
It is not necessarily an [[antipattern]] to use a [[switch statement]] within a loop—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 [[inversion of control]] such as 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 [[event-driven programming]], [[event loop]] and [[event-driven finite state machine]]).
Note that this is not a performance antipattern, though it may lead to an inconsequential performance penalty due to the lack of an [[Loop unwinding|unrolled loop]]. Rather, this is a clarity antipattern, as in any non-trivial example, it is much more difficult to decipher the intent and actual function of the code than the more straightforward refactored solution.
 
Note that thisThis is not a performance antipattern, though it may lead to an inconsequential performance penalty due to the lack of an [[Loop unwinding|unrolled loop]]. Rather, thisit is a clarity antipattern, as in any non-trivial example, it is much more difficult to decipher the intent and actual function of the code than the more straightforward refactored solution.
 
==Example==
Here is an example of the antipattern:
 
An event-driven solution would implement a [[Observer pattern|listener interface]]:
<sourcesyntaxhighlight lang="java">
String key = null;
String value = null;
List<String> params = null;
int column = 0;
 
public void addToken(token) {
 
// parse a key, a value, then three parameters
switch (column) {
case 0:
params = new LinkedList<String>();
key = token;
break;
case 1:
value = token;
break;
default:
params.add(token);
break;
}
if (++column >= 5) {
column = 0;
completeRow(key, value, params);
}
}
</syntaxhighlight>
 
HereBut iswithout the listener, it becomes an example of the antipattern:
<sourcesyntaxhighlight lang="java">
// parse a key, a value, then three parameters
String key = null;
Line 27 ⟶ 58:
}
}
</syntaxhighlight>
</source>
 
And here is the refactored solution:
<syntaxhighlight lang="java">
 
<source lang="java">
// parse a key and value
String key = stream.parse();
Line 38 ⟶ 68:
// parse 3 parameters
List<String> params = new LinkedList<String>();
for (int i = 20; i < 53; i++) {
// in practice, that might be written as (i = 0; i < 3; i++),
// but for illustration we use the same range of indices here
// as in the switch above; the fact that the value of i itself
// is not actually used anywhere in the loop body below is what
// proves that the loop-and-switch construct was superfluous
// in the example above.
params.add(stream.parse());
}
</syntaxhighlight>
</source>
 
==References==
<references />
 
[[Category:Anti-patternsComputer programming]]