Monday, August 29, 2005

Magic Numbers

Our code guidelines state clearly that "magic number" are not permitted. We run Checkstyle to pick up style related violations. So, despite this, what do I find when I'm poking around fixing something unrelated?


private static final int PARAM_INDEX_THREE = 3;
private static final int PARAM_INDEX_FOUR = 4;
private static final int PARAM_INDEX_FIVE = 5;
private static final int PARAM_INDEX_SIX = 6;
private static final int PARAM_INDEX_SEVEN = 7;
private static final int PARAM_INDEX_EIGHT = 8;
private static final int PARAM_INDEX_NINE = 9;
private static final int PARAM_INDEX_TEN = 10;

The values above were used to index the result of this:

String [] fields = record.split ("\\|");

Ignore the fact that a pipe separated string might not be the ideal way to send this data. That's another topic altogether. Ignore that fact that elements 0, 1 and 2 were indexed with hard coded numbers. I still don't fathom what was the point of going to this much effort and still not giving the constants meaningul names. And yes, the code used these to index the array and set the values to attributes of the class.

Thursday, August 25, 2005

Freedom Languages

A thought provoking article over at Code Craft. Kevin compares languages that try to remove constructs that are deemed confusing or easy to misuse with languages that try to remove constructs that reduce programmer freedom. According to Kevin, C++, Java, C are examples of safety languages; his freedom languages include Python and Ruby.

For what it's worth, I find programming in a safety language (C++) as satisfying as writing in a freedom language (Ruby). In a sense, I feel both do their job well. Somehow, I can never get real satifaction from writing Java (OK, it pays the bills, but I mean that pleasure of writing something beautiful; something that has merit beyond its practical purpose). The C++ language has beauty in its austerity. Ruby has a different charm, but ultimately the attraction of both is their power to let me express the problem without hiding it in the messy housekeeping details. My first reaction when I discovered C++ (back in the old cfront days) was that it improved the signal to noise ratio (that's my time in HW slipping through the cracks). And there's my chief complaint about Java; I spend too much time (and too many lines of code) writing things that are better left unseen. In trying to make the laguage safe, it has made it more difficult to write and maintain.

If Java's greatest strength is that it is easy enough that anyy programmer can write it; that is also its greatest weakness. Today we have far too many people who can write Java (and are Java Certified Engineers) but don't know how to program.

Category: Software, Programming, Java, Ruby