Jump to content

Loop-switch sequence

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 128.105.175.26 (talk) at 19:10, 2 November 2006 (Added a missing ) in the refactored solution example). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Loop/Switch sequence

A specific derivative of the spaghetti code programming antipattern which achieves it's crapulence from the obfuscating of a clear process of steps with a byzantine switch-within-a-loop idiom.

Example of Antipattern (pseudocode)

  String key;
  String value;
  List<String> 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)

  String key = stream.parse();
  String value = stream.parse();
  List<String> args;
  
  for ( int i = 0; i < 3; ++i )
  {
    args.add( stream.parse() );
  }