Article 38P7G CodeSOD: Arrject

CodeSOD: Arrject

by
Remy Porter
from The Daily WTF on (#38P7G)

There are some submissions that we get, and we simply sit on, because there's nothing much to say about them. They're awful code, but there's no major comments to be added. It's clear and simple in its awfulness.

For example, you have some code that needs to display details about colleges around the US. Each College has a name, a full name, a short name, a state and city where it exists, and full names for those states and cities. You are likely reaching for an object to store that information, but why do that, when you can employ what I call the "Arrject Pattern". Y'know, when instead of using objects, you use multiple arrays and store related data at the same index? Stuff like what Kevin found in his codebase:

var schoolNames = new Array();var schoolFullNames = new Array();var schoolShortNames = new Array();var schoolStates = new Array();var schoolCities = new Array();var schoolStateFullNames = new Array();var schoolCityFullNames = new Array();schoolNames[0] = "fsu";schoolFullNames[0] = "Florida State University";schoolShortNames[0] = "FSU";schoolStates[0] = "fl";schoolCities[0] = "tallahassee";schoolStateFullNames[0] = "Florida";schoolCityFullNames[0] = "Tallahassee";schoolNames[1] = "tamu";schoolFullNames[1] = "Texas A&M University";schoolShortNames[1] = "Texas A&M";schoolStates[1] = "tx";schoolCities[1] = "college-station";schoolStateFullNames[1] = "Texas";schoolCityFullNames[1] = "College Station";schoolNames[2] = "txstate";schoolFullNames[2] = "Texas State University";schoolShortNames[2] = "TX State";schoolStates[2] = "tx";schoolCities[2] = "san-marcos";schoolStateFullNames[2] = "Texas";schoolCityFullNames[2] = "San Marcos";schoolNames[3] = "uaz";schoolFullNames[3] = "University of Arizona";schoolShortNames[3] = "U of A";schoolStates[3] = "az";schoolCities[3] = "tuscon";schoolStateFullNames[3] = "Arizona";schoolCityFullNames[3] = "Tuscon";schoolNames[4] = "ucf";schoolFullNames[4] = "University of Central Florida";schoolShortNames[4] = "UCF";schoolStates[4] = "fl";schoolCities[4] = "orlando";schoolStateFullNames[4] = "Florida";schoolCityFullNames[4] = "Orlando";schoolNames[5] = "ufl";schoolFullNames[5] = "University of Florida";schoolShortNames[5] = "UF";schoolStates[5] = "fl";schoolCities[5] = "gainesville";schoolStateFullNames[5] = "Florida";schoolCityFullNames[5] = "Gainesville";schoolNames[6] = "uiuc";schoolFullNames[6] = "University of Illinois at Urbana-Champaign";schoolShortNames[6] = "U of I";schoolStates[6] = "il";schoolCities[6] = "urbana-champaign";schoolStateFullNames[6] = "Illinois";schoolCityFullNames[6] = "Urbana-Champaign";schoolNames[7] = "uky";schoolFullNames[7] = "University of Kentucky";schoolShortNames[7] = "UK";schoolStates[7] = "ky";schoolCities[7] = "lexington";schoolStateFullNames[7] = "Kentucky";schoolCityFullNames[7] = "Lexington";schoolNames[8] = "mizzou";schoolFullNames[8] = "University of Missouri";schoolShortNames[8] = "MIZZOU";schoolStates[8] = "mo";schoolCities[8] = "columbia";schoolStateFullNames[8] = "Missouri";schoolCityFullNames[8] = "Columbia";schoolNames[9] = "usc";schoolFullNames[9] = "University of Southern California";schoolShortNames[9] = "USC";schoolStates[9] = "ca";schoolCities[9] = "los-angeles";schoolStateFullNames[9] = "California";schoolCityFullNames[9] = "Los Angeles";schoolNames[10] = "ut";schoolFullNames[10] = "University of Texas at Austin";schoolShortNames[10] = "UT Austin";schoolStates[10] = "tx";schoolCities[10] = "austin";schoolStateFullNames[10] = "Texas";schoolCityFullNames[10] = "Austin";schoolNames[11] = "utsa";schoolFullNames[11] = "University of Texas at San Antonio";schoolShortNames[11] = "UTSA";schoolStates[11] = "tx";schoolCities[11] = "san-antonio";schoolStateFullNames[11] = "Texas";schoolCityFullNames[11] = "San Antonio";function showSearchResults(event, searchval) { var numResults = 0; var key = (event.keyCode || event.which); var results = ""; var firstresult = ""; if(searchval.length > 0) { var searchvalfixed = searchval.toLowerCase(); if(searchvalfixed.indexOf("the") == 0) { searchvalfixed = searchvalfixed.substr(3); } searchvalfixed = searchvalfixed.trim(); for(var i = 0; i < schoolNames.length; i++) { if(schoolFullNames[i].toLowerCase().indexOf(searchvalfixed) != -1 || schoolShortNames[i].toLowerCase().indexOf(searchvalfixed) != -1 || schoolCities[i].toLowerCase().indexOf(searchvalfixed) != -1 || schoolStates[i].toLowerCase().indexOf(searchvalfixed) != -1 || schoolStateFullNames[i].toLowerCase().indexOf(searchvalfixed) != -1 || schoolShortNames[i].toLowerCase().indexOf(searchvalfixed) != -1 || schoolCityFullNames[i].toLowerCase().indexOf(searchvalfixed) != -1) { numResults++; results += "<a href=\"javascript:fillSearch('" + schoolNames[i] + "','" + schoolFullNames[i] + "');\" >" + schoolFullNames[i] + "</a><br>"; if(firstresult.length == 0) { firstresult = schoolNames[i]; } } } if(numResults){ $("#resultsContainer").fadeIn("slow"); document.getElementById("resultsContainer").innerHTML = results; } else { $("#resultsContainer").fadeOut("slow"); } } else { $("#resultsContainer").fadeOut("slow"); } if (key==13 && firstresult.length > 0){ location.href = firstresult; }}function fillSearch(shortName,fullName){ document.getElementById("search_landing").value = ''; document.getElementById("search_landing").value = fullName; document.getElementById("search_shortname").value = shortName; $("#resultsContainer").fadeOut();}function goToSchool(){if(document.getElementById("search_shortname").value)location.href = document.getElementById("search_shortname").value;}

It's worth noting that this was inlined in the HTML file, up in the header, and not included from a separate file.

buildmaster-icon.png [Advertisement] Application Release Automation - build complex release pipelines all managed from one central dashboard, accessibility for the whole team. Download and learn more today! TheDailyWtf?d=yIl2AUoC8zATf4zd5HYO6g
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments