This sort of thing might push me to Scala

Gordon Milne <[email protected]> Fri, 18 Dec 2009 10:48:39 +1300
Newsgroups gmane.comp.programming.language-of-the-year
Message-ID <[email protected]>
Hi!

I have 4 methods that have code like this in them:

    final LsWaitingCursor waitingCursor = new LsWaitingCursor(frame);
    try {
        new MethodEditor(title, file);
    }
    finally {
        waitingCursor.disable();
    }

The only thing difference between the 4 methods is the code in the try
block. That piece of code is always a single statement. What I really
want to write is this:

    runWithWaitCursor { new MethodEditor(title, file) }

Since I work in Java, I settled for this:

    runWithWaitCursor(runnable).

The only problem is that this code is just as verbose and
boiler-plated as before, since I end up writing:

    runWithWaitCursor(new Runnable()
    {
       public void run()
       {
           new MethodEditor(title, file);
       }
    });

How many lines of code did I just save? Zero and I now have another
method (runWithWaitCursor) which also has 7 lines of code in it. So my
neat idea hasn't reduced my verbosity. It has increased it! And now I
have an extra 7 lines of code in runWithWaitCursor().

The only benefit I did gain was that I separated out the execution of
a task (e.g. new MethodEditor()) from a change in the appearance of
the cursor (i.e. waiting/normal).

It doesn't seem like a big enough benefit to me.

So, I'm going to revert this change. And dig deeper into the
Odersky/Spoon/Venners book.

regards,

Gordon J Milne
--------------
I struggle because it's good for me!