Loop-switch Sequence
   HOME

TheInfoList



OR:

A loop-switch sequence (also known as the for-case paradigmThe FOR-CASE paradigm
an
Switched on Loops
at The Daily WTF or Anti-
Duff's Device In the C programming language, Duff's device is a way of manually implementing loop unrolling by interleaving two syntactic constructs of C: the - loop and a switch statement. Its discovery is credited to Tom Duff in November 1983, when Duff was w ...
) 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 Spaghetti code is a pejorative phrase for difficult-to- maintain and unstructured computer source code. Code being developed with poor structure can be due to any of several factors, such as volatile project requirements, lack of programming style ...
. It is not necessarily an antipattern to use a
switch statement In computer programming languages, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map. Switch statements function ...
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 In computer programming, event-driven programming is a programming paradigm in which the Control flow, flow of the program is determined by external Event (computing), events. User interface, UI events from computer mouse, mice, computer keyboard, ...
,
event loop In computer science, the event loop (also known as message dispatcher, message loop, message pump, or run loop) is a programming construct or design pattern that waits for and dispatches events or messages in a program. The event loop works by m ...
and event-driven finite state machine). This is not a performance antipattern, though it may lead to an inconsequential performance penalty due to the lack of an unrolled loop. Rather, it 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

An event-driven solution would implement a listener interface: String key = null; String value = null; List params = null; int column = 0; public void addToken(token) But without the listener, it becomes an example of the antipattern: // parse a key, a value, then three parameters String key = null; String value = null; List params = new LinkedList(); for (int i = 0; i < 5; i++) And here is the refactored solution: // parse a key and value String key = stream.parse(); String value = stream.parse(); // parse 3 parameters List params = new LinkedList(); for (int i = 0; i < 3; i++) { params.add(stream.parse()); }


References

Computer programming