Loop-switch sequence

Loop-switch sequence

A loop-switch sequence is a specific derivative of the spaghetti code programming antipattern where a clear set of steps is implemented as a byzantine switch-within-a-loop. Also known as "The FOR-CASE paradigm" [http://thedailywtf.com/Articles/Switched_on_Loops.aspx] .

Note: it is not necessarily an antipattern to use a switch statement within a loop. It is only 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). Also regarding the alarm that could trigger the possible performance loss that 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.

Example of antipattern (pseudocode)

// parse a key, a value, then three parameters

String key; String value; List 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 args; for ( int i = 0; i < 3; ++i ) { args.add( stream.parse() ); }


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • Loop (instruction) — Structure de contrôle En programmation impérative, une structure de contrôle est une commande qui contrôle l ordre dans lequel les différentes instructions d un algorithme ou d un programme informatique sont exécutées. On appelle aussi cet… …   Wikipédia en Français

  • Crossbar switch — In electronics, a crossbar switch (also known as cross point switch, crosspoint switch, or matrix switch) is a switch connecting multiple inputs to multiple outputs in a matrix manner. Originally the term was used literally, for a matrix switch… …   Wikipedia

  • Anti-pattern — For the book, see AntiPatterns. In software engineering, an anti pattern (or antipattern) is a pattern that may be commonly used but is ineffective and/or counterproductive in practice.[1][2] The term was coined in 1995 by Andrew Koenig,[3]… …   Wikipedia

  • Antipatrón de diseño — Saltar a navegación, búsqueda Un antipatrón de diseño es un patrón de diseño que invariablemente conduce a una mala solución para un problema. Al documentarse los antipatrones, además de los patrones de diseño, se dan argumentos a los diseñadores …   Wikipedia Español

  • Structure de contrôle — En programmation impérative, une structure de contrôle est une commande qui contrôle l ordre dans lequel les différentes instructions d un algorithme ou d un programme informatique sont exécutées. On appelle aussi cet enchaînement d instructions… …   Wikipédia en Français

  • Boucle (informatique) — Structure de contrôle En programmation impérative, une structure de contrôle est une commande qui contrôle l ordre dans lequel les différentes instructions d un algorithme ou d un programme informatique sont exécutées. On appelle aussi cet… …   Wikipédia en Français

  • Boucle for — Structure de contrôle En programmation impérative, une structure de contrôle est une commande qui contrôle l ordre dans lequel les différentes instructions d un algorithme ou d un programme informatique sont exécutées. On appelle aussi cet… …   Wikipédia en Français

  • Branchement conditionnel — Structure de contrôle En programmation impérative, une structure de contrôle est une commande qui contrôle l ordre dans lequel les différentes instructions d un algorithme ou d un programme informatique sont exécutées. On appelle aussi cet… …   Wikipédia en Français

  • Do (instruction) — Structure de contrôle En programmation impérative, une structure de contrôle est une commande qui contrôle l ordre dans lequel les différentes instructions d un algorithme ou d un programme informatique sont exécutées. On appelle aussi cet… …   Wikipédia en Français

  • Else (instruction) — Structure de contrôle En programmation impérative, une structure de contrôle est une commande qui contrôle l ordre dans lequel les différentes instructions d un algorithme ou d un programme informatique sont exécutées. On appelle aussi cet… …   Wikipédia en Français

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”