Re: This sort of thing might push me to Scala

"Michael T. Nygard" <[email protected]> Fri, 18 Dec 2009 11:09:04 -0600
Newsgroups gmane.comp.programming.language-of-the-year
Message-ID <[email protected]>
Gordon,

This kind of repeated coding pattern is definitely easier to express in either Scala or Clojure.  

It seems like you have an abstraction trying to escape from the boilerplate code.  Perhaps capturing that abstraction structurally (i.e., class and interface) rather than procedurally (as a coding pattern) would also help.

The abstraction that I see is the idea of "a modal change over a duration".  What if you explicitly represent the long-running task as an object of it's own?  In that case, either the lifecycle of the task object itself, or a decorator to the task object, could address the global state change.  One flavor of global state change would be setting the wait cursor.  You might find a use for other kinds of global state change tied to the lifecycle of the task.

For instance, in one of my applications, we not only set a wait cursor, but also threw a GlassPane over the whole UI so that mouse input was ignored during the task.

Cheers,
-Michael Nygard


On Dec 17, 2009, at 3:48 PM, Gordon Milne wrote:

> 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!
> 

Michael T. Nygard
[email protected]
http://www.michaelnygard.com/

Release It! Design and Deploy Production-Ready Software
http://bit.ly/ReleaseIt

Beautiful Architecture
http://bit.ly/BeautifulArchitecture

97 Things Every Software Architect Should Know
http://bit.ly/97Things