Programmers like to program. This shouldn’t be much of a surprise to anyone. Unfortunately, sometimes they program when they shouldn’t. Modern development platforms and languages are packed with useful functions in their libraries, but too many developers never bother to learn what’s in those libraries, so they waste time implementing (usually badly) functionality already available.
Punishing initiative is counterproductive, but programmers need to do a little research–just a quick web search–before banging out code.
Printing an array in Java
This code wasn’t intended for production but was written to help with debugging during development. It ended up in the production system as part of the log generation process. (You should always be careful when writing code that you aren’t planning on keeping. It has a strange way of sticking around.) Its purpose was to print out an array of integers.
public static void dumpArray(int [] a)
{
System.out.print("{ ");
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println("}");
}
When the logging mechanism in the product was upgraded so that it was configurable instead of simply sending everything to the console, this little piece was found since it was hard-coded to send data to System.out. Not only was it now useless, but it had been a bad idea from the start. Why? Because this functionality already exists in the java.util.Arrays class. There are a whole set of toString functions for converting arrays of primitive types into strings. The above function could have been replaced with:
System.out.println(java.util.ArraystoString(a));
Creating a temporary file in ColdFusion
In a completely different system, the Captain came across some code that was attempting to create a server-side temporary file and write some data to that file. Since the code was server-side, it was trying really hard to guarantee that the generated file name was unique.
<cfset bad_name = True>
<cfset time_part = GetTickCount()>
<cfset rand_part = RandRange(1, 100000>
<cfset iter_part = 0>
<cfloop condition = "bad_name"
<cfset filename = #request.fileserver# &
"#time_part#_#rand_part#_#iter_part#.tmp">
<cfset bad_name = FileExists(filename)>
<cfset iter_part = iter_part + 1>
</cfloop>
The code tries to generate a unique file name by concatenating a time value, a random value, and iterable value. If the file name exists, it increments the value of the iterable variable and tries again. It keeps checking until it finds a file name that does not currently exist. The code worked. In fact, it worked for a very long time as part of the system. It’s just unnecessary code, because ColdFusion already provides a function to create temporary files.
<cfset filename = GetTempFile("#request.fileserver#","TMP")>
Using the library function removed nine lines of code and a loop from the ColdFusion component.
Retrieving a file from a web server with Python
The worse instance of this problem the Orc Captain has come across was from developer who was tasked to write some Python code. A dynamic web page was generating statistics concerning the functioning of a particular process. The task was to retrieve and parse this information and then combine it with data from a few other servers to generate a report. It was just a quick and dirty internal app that was already nearly complete.
The developer in question was a genius when it came to writing low-level server code. Having just finished a large project, he was given this task as a break from the heavy lifting he’d been responsible for over the previous months. What he did was painful. Unfamiliar with the language but with years of experience in network programming, he looked for and found the socket library. A sanitized and clipped fragment of his code is below.
import socket
message = """GET / HTTP/1.1
example.com
"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("example.com", 80))
s.send(message)
data = s.recv(1024)
# Code to parse and check the response header
# from the HTTP call and take action
# Code to parse out the how much content is available
# Code to read the content
s.close()
He sent the HTTP call by hand through a socket and wrote a very simple HTTP library to handle the results. The most robust solution would be to use Python’s httplib, which can handle redirects and such, but you can pull down a web page in three lines if you’re in a hurry.
import urllib
page = urllib.urlopen("http://example.com")
contents = page.read()
page.close()
