Cleaning Up the Java Code a Bit

Code Clean Up

This morning I was taking another pass at the Java client code for my Ticker Plants. The problem was that the original author didn't have any real experience with Java, and while it works, it's not working how it really should. Take as an example, the use of threads. In boost, which was the source for the Java port, creating threads is very simple:

  t = boost::thread(&MyClass::method, this, arg1);

and away it goes. Very simple, very clean.

In Java you can go the Thread or the Runnable route, and what had happened was the coder had created subclasses of Thread whose constructor took a class instance and then the run() method was simply:

  public void run() {
    mClass.unspool();
  }

where the unspool() method on the class does all the looping, etc. to get the job done. The problem is, there are literally dozens of lines of code overhead to accomplish this. I went in and simplified this to look like:

  mUnspoolThread = new Thread(new Runnable() {
                                 public void run() {
                                     unspool();
                                 }
                             }, "Unspooler");
  if (mUnspoolThread != null) {
    mUnspoolThread.setDaemon(true);
    mUnspoolThread.start();
  }

and this allowed me to drop probably 180 lines of code. It was pretty shocking.

I'm not done, but with every change I've gone through the code and tests to make sure I haven't changed the behavior any, and so far it's clean. I just need to keep going over it and over it to get it to the point that it's ready to really be used for the kinds of applications we have.