Java: All Syntax and No Semantics

6 AM October 21, 2005

Some days, Java is all syntax and no semantics. Today I coded seven levels of indentation in twenty-five lines:

    public void onLogUpdated() {

        beginUse();

        try { 

            synchronized (this) {

                final SubscriberMessage next = log.getCurrent();

                if (next == last) {

                    return;

                }

                last = next;

                new SafeRunnable() {

                    protected void runProtected() throws Exception {

                        if (doesTone) {

                            soundTone(next);

                        }

                        if (doesAnnounce) {

                            soundAnnounce(next);

                        }

                    }

                }.start();

            }

        } finally {

            endUse();

        }

    }

A few lines of actual functionality and the rest is big heavy structure and great gobs of static typing. Normally when I end up with a scrappy method like this, I can see a simple way to make more understandable - but not today. Anyone have a suggestion for making this method nicer to look at?

Perhaps a cold beverage will help.

By alang | # | Comments (6)
(Posted to javablogs, Java and Rants)

Comments

At 09:01, 21 Oct 2005 Mikkel wrote:

Well, you could make a subclass of SafeRunnable to make the instantiation more pretty. And you could synchronize the whole method given that beginUse() and endUse() does not contain a lot of work.

Cheers
Mikkel

    public void onLogUpdated() {
        beginUse();
        try { 
            final SubscriberMessage next = log.getCurrent();
            if (next != last) {
                new MessageAnnouncer(next).start();
            }
        } finally {
            endUse();
        }
    }
(#)
At 09:27, 21 Oct 2005 Simon Brunning wrote:

He could, Mikkel, but I think the cold beer was a better idea.

(#)
At 16:51, 21 Oct 2005 Ron wrote:

ahhhh beer mmmmmmmmmmmmmmmmmmmmmmmmmmm

(#)
At 06:21, 22 Oct 2005 Kirk wrote:

It's difficult to restructure this without the larger context but here are some suggestions that may or may not be valid.

If looks as if you have different kinds of SubscriberMessages. Some make noises and some make announcements. You could define SubscriberMessages as an interface

public interface SubscriberMessage() {

   public void soundTone();
   public void soundAnnounce();

....

}

So now you may or may not (depending on the division of the leftover functionalily in the concrete class) have to define most likley an AbstractCalls that declares soundTone() and soundAnnounce() as abstract methods. These would be defined by their concrete subclasses polymophicly (i.e.) the methods may have behavor or they maybe empty. Now;

public void runProtected() {
    soundTone();
    soundAnnounce();
}

Difficult to tell from this code fragment but my guess is that the functionality in the synchronized block does not belong in this method and that it should be pushed into whatever log is. I say this because it looks like it's directly manipulating a list of some sorts and log appears to be that list. At any rate, this method is doing two things, adjusting a list and creating and running a seperate execution context. It should do one of these but not both.

Again, difficult to say with just a fragment and no other context.

HTH,
Kirk

(#)
At 15:59, 26 Oct 2005 Nils wrote:

I find anonymous inner classes to be mostly unnecessary, this being a good example. SafeRunnable should be a stand-alone class definition, optionally a named inner class, with the needed variables passed to the constructor.
You could also refactor the code inside the synchronized block into a private method and make that method synchronized.

(#)
At 19:53, 01 Dec 2005 Tom Hawtin wrote:

You could use java.util.concurrent.locks, or similar, to combine the synchronized with the beginUse/endUse.

(#)

Add Comment




(Not displayed)






(Leave blank line between paragraphs. URLs converted to links. HTML stripped. Indented source code will be formatted with <pre> tags.)




© 2003-2006 Alan Green