Hidden costs of American Programmers

Slashdot recently had an article on the hidden costs of outsourcing programming work overseas. One of the posted an example of some god-awful code that his company got from their H1B programmers. You get what you pay for. Ignoring for the moment that H1B's aren't actually outsourced contractors, let's take a look at the code, which is supposed to parse ZIP and ZIP+4 zip codes (apparently coming from a web request):

String zip = new String(req.getParameter("ZIP"));

// several lines deleted for clarity

StringTokenizer ziptk = new StringTokenizer(zip, "-"); int zipcount = ziptk.countTokens(); String zip1 = null; String zip2 = null; switch (zipcount) { case 2: while (ziptk.hasMoreElements()) { zip1 = (String) ziptk.nextElement(); userBean.setZip(zip1); zip2 = (String) ziptk.nextElement(); userBean.setZip1(zip2); } case 1: while (ziptk.hasMoreElements()) { zip1 = (String) ziptk.nextElement(); userBean.setZip(zip1); userBean.setZip1(""); } }

The poster has a hilarious deconstruction of this code in his comment, but I read it and said, "Hell, we've got worse code in our software, written by American programmers!"

Check it out. This code parses American-style dates from the web.

int day1 = 1;
int month1 = 0;
int year1 = 1970;
// Parse start date
StringTokenizer st = new StringTokenizer(fromDate, "/");
if (st.countTokens() == 3) {
	Integer i = new Integer(st.nextToken());
	month1 = i.intValue();
	month1--; //months are numbered from 0 to 11
	i = new Integer(st.nextToken());
	day1 = i.intValue();
	i = new Integer(st.nextToken());
	year1 = i.intValue();
}
GregorianCalendar cal = new GregorianCalendar(year1, month1, day1, 0, 0, 0);
Date startDate = cal.getTime();
int day2 = 1;
int month2 = 0;
int year2 = 1970;
st = new StringTokenizer(toDate, "/");
if (st.countTokens() == 3) {
	Integer i = new Integer(st.nextToken());
	month2 = i.intValue();
	month2--; //months are numbered from 0 to 11
	i = new Integer(st.nextToken());
	day2 = i.intValue();
	i = new Integer(st.nextToken());
	year2 = i.intValue();
}
cal = new GregorianCalendar(year2, month2, day2, 23, 59, 59);
Date endDate = cal.getTime();

  1. Why aren't you using a DateFormat...considering the format you need is defined at the top of the class?
  2. No accounting for any sort of errors in the date parameter. At least a default date is set up!
  3. Once you've decided to parse the date yourself, did you really have to copy and paste the code to do it again?

God knows my own code isn't perfect, but there is just no substitute for knowing the libraries of the system you're using -- where ever you're from.