Keep the bus interested

11 PM February 26, 2006

A colleague just pointed out that public transport in Sydney is cheap, provided you keep the bus interested:

… cash fares are an inexpensive way to make single trips. Tickets are purchased from the driver each time you bored the bus.

So: wear bright clothes, have a couple of jokes handy, and – whatever you do – don’t go moaning about the queues.

By alang | # | Comments (0)
(Posted to Tall Tales and Stuff)

Complex problems require skilled programmers

4 AM February 8, 2006

Writing maintainable code requires a balancing act between making each line of code simple and keeping each method concise. In my current project, I keep choosing conciseness over apparent simplicity, because I don't think making it look simple would help.

For example, the following function schedules a task for a single user:


private void scheduleTask(final User user) {

    int waitMs = getWaitMs(user);

    timer.schedule(new TimerTask() {

        public void run() {

            new SafeRunnable() {

                protected void runProtected() {

                    checkFor(user);

                }

            }.start();

        }

    }, Utils.randInt(waitMs), waitMs);

}

(Actually, the real code has some comments to explain the implementation a little. The method uses the standard java.util.Timer class to initiate the checks. Since the checkFor() method does a small amount of I/O, a new thread needs to be spawned in order to keep the timer thread responsive. SafeRunnable is our own threading utility class that prevents unexpected runtime exceptions from terminating the underlying thread pool. The random initial wait time ensures that checks for multiple users having the same wait time don't occur all at once.)

I did consider refactoring this method into named inner classes, or even regular classes, because the vast majority of Java programmers throw up their hands and say, "huh?", at the sight of double-nested anonymous classes.

However, it seemed best to put all the code in one place, because it will save jumping back and forth in the editor when reading the code later (during debugging or enhancements). I don't feel the need to make the code more accessible to programmers who aren't comfortable with anonymous inner classes: I suspect that they wouldn't understand the threading issues anyway.

It's a trade-off that makes me feel like I'm being arrogant, but at the end of the day, complex problems require skilled programmers.

By alang | # | Comments (2)
(Posted to javablogs, Java and Rants)
© 2003-2006 Alan Green