When digging into a newly inherited codebase, most experienced developers have a preferred way to break the system down into understandable pieces. Most of course start with the documentation, but since there rarely is any, that leaves one of two approaches: back-end to front-end or front-end to back-end. John, a Java and SQL developer, generally likes to start in the database and work his way to the front-end.

Recently, he drew the short straw and was tasked with making a few changes to a legacy Swing application that everyone was afraid to touch. Having never seen the innards of the app, he dove straight to the database and noticed a rather interest querying pattern.

SELECT TDate = TDate +"|",
       ID = "0000000"+"|", -- space(7)
       Reference = "            "+"|", -- space(12)
       ...
       Price = REPLICATE("0", 19 - CHAR_LENGTH(
                   CONVERT(VARCHAR,
		       CONVERT(DECIMAL(19,9),
		           Value/Units)))) 
             + CONVERT(VARCHAR(17),
	           CONVERT(DECIMAL(19,9),
		       Value/Units)) 
             + "|",
       ...
  FROM Detail

Just about every query seemed to return data as a fixed-width string with a pipe character appended to it. Perhaps it was used in some sort of pipe-separated import system? he wondered, maybe it needed to be in that format for transport? He figured there just had to be some reason it was coded like that.

Jumping over to the Java side to see what the data-access layer was doing fulfilled his curiosity.

Object[] tableData = new Object[31];
dTableRef.setNumRows(0);
while (jobListRs.next()) {
	tableData[0] = getSubstring(jobListRs.getString(1));
	tableData[1] = getSubstring(jobListRs.getString(2));
	tableData[2] = getSubstring(jobListRs.getString(3));
	tableData[3] = getSubstring(jobListRs.getString(4));
	
	... 4 ... 5 ... 6 ... ... ... 28
	
	tableData[29] = getSubstring(jobListRs.getString(30));
	tableData[30] = getSubstring(jobListRs.getString(31));
	dTableRef.addRow(tableData);
}
jobList.close();

That's right, there was no good reason. It simply dumped the data into an object array, which was then used by the front-end to display it. As for the getString() method, it too reinforced John's "no reason" conclusion.

public String getSubstring(String s) {
	int reqLength = s.length() - 1;

	String newString = s.substring(0, reqLength);

	return newString;
}

At least now John is intimately familiar with why no one wants to work on the application.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!