Monday, January 23, 2006

Syntactic Sugar
I really like some of the new features in the language for JDK 1.5. Generics, autoboxing, and annotations can make code easier to write and maintain. However, Java has implemented a number of these simply as syntactic sugar, rather than as a real change to the environment. This was important for maintaining compatibility, but sometimes it gets annoying.

While looking for a bug today (it turned out to be a typo that happened to compile) I decided to print out various forms of Unicode. These included a char[], a Character[], and a List<Character>. The code to print these used the new looping construct:

  void print(char[] c) {
for (char x: c) System.out.print(x);
System.out.println();
}

void print(Character[] c) {
for (Character x: c) System.out.print(x);
System.out.println();
}

void print(java.util.List c) {
for (Character x: c) System.out.print(x);
System.out.println();
}
Notice how the difference between each method is trivial? If Java used templates in a similar way to C++, then this would be one method. Yes, I know the compiler creates 3 methods in the binary. But why would I care about code redundancy on that scale when Universal binaries make duplicates of every piece of executable code? These days we need space for data, and almost never for code (even embedded applications typically have megabytes available to use).

So generics are nice, but when they can't be used in methods like these, they feel too restrictive.

The other thing that bothers me, is that the only way to describe management of an array is to create static methods that take arrays as parameters, and/or return arrays. Java arrays have no inherent extensibility. Sometimes I find that it would be useful to complement certain classes with another class that holds an array of the first class. Unfortunately, these classes are not compatible with real arrays. At least lists are there to take up some of the slack, but ArrayList can only get you so far.

I'm probably just a little sensitive to the inflexibility of arrays, as I've been (finally) learning some Python, where life is definitely better for arrays. I therefore found it amusing that one of my responses to the sort/unique problem of the other day was from Stephen where he showed of his 1337 h4x0r skillz with Python to solve the problem in 2 lines using built-in language features. Maybe I should be moving to Jython. :-)

Family Life
Anne has recently decided to put up a web page on our domain. She's using iWeb and I've been impressed with how quickly she was able to put together a site that looks reasonably attractive. I just hope it doesn't look like the web site of everyone else who uses iWeb. :-)

This will only be of interest to people who know us personally, as it is very focused on our family. I have no control over content, though I have been encouraged to read it myself. I think Anne just wants someone to proof read. ;-)

1 comment:

Stephen Thorne said...

To be fair, I have to post the complete version of the code - including everything it's 50 lines of code.

Jython is okay, but you should be aware that it's an incomplete implementation of python2.2, and both the functions I used to compose that solution were new in python2.4.

Prior to python2.4, you could only sort a list in place, i.e. mylist.sort() was an in-place sort. Now the 'sorted' builtin has been introduced that takes an arbitary sequence and returns a sorted list.

Also, python2.3 had the 'sets' module, which was so popular that it got included in the builtins as 'set' in python2.4.

http://thorne.safenetbox.biz/~stephen/uniq.py
is my complete module, which has been tested in python1.5, 2.2, 2.3 and 2.4.