Syntax coloring

terça-feira, 1 de março de 2011

An example of anti-code

When doing some code review, I've faced this:
var stateOptions = null;
var typeOptions = null;

function hideOptions() {
 if(stateOptions == null) {
  stateOptions = new Array();
  stateOptions[0] = document.getElementById('status').options[0];
  stateOptions[1] = document.getElementById('status').options[1];
  stateOptions[2] = document.getElementById('status').options[2];
  stateOptions[3] = document.getElementById('status').options[3];
  stateOptions[4] = document.getElementById('status').options[4];
  stateOptions[5] = document.getElementById('status').options[5];
 }
 document.getElementById('status').remove(1);
 document.getElementById('status').remove(1);
 document.getElementById('status').remove(1); 
}

function showOptions() {
 document.getElementById('status').remove(0);
 document.getElementById('status').remove(0);
 document.getElementById('status').remove(0);
 try {
  
  document.getElementById('status').add(stateOptions[0], null);
  document.getElementById('status').add(stateOptions[1], null);
  document.getElementById('status').add(stateOptions[2], null);
  document.getElementById('status').add(stateOptions[3], null);
  document.getElementById('status').add(stateOptions[4], null);
  document.getElementById('status').add(stateOptions[5], null);
  
 } catch(ex) {
  document.getElementById('status').add(stateOptions[0], 0);
  document.getElementById('status').add(stateOptions[1], 1);
  document.getElementById('status').add(stateOptions[2], 2);
  document.getElementById('status').add(stateOptions[3], 3);
  document.getElementById('status').add(stateOptions[4], 4);
  document.getElementById('status').add(stateOptions[5], 5);
 } 
}
...

OMG