Loop-switch sequence
A loop-switch sequence[1] (also known as the for-case paradigm[2] 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 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).
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<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);
}
}
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<String> params = new LinkedList<String>();
for (int i = 0; i < 5; i++) {
switch (i) {
case 0:
key = stream.parse();
break;
case 1:
value = stream.parse();
break;
default:
params.add(stream.parse());
break;
}
}
And here is the refactored solution:
// parse a key and value
String key = stream.parse();
String value = stream.parse();
// parse 3 parameters
List<String> params = new LinkedList<String>();
for (int i = 0; i < 3; i++) {
params.add(stream.parse());
}
References
- ^ "Loop-switch sequences". LEVEL UP CODE. 30 November 2013. Retrieved 11 April 2016.
- ^ The FOR-CASE paradigm and Switched on Loops at The Daily WTF
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.