Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #3737

Re: Weird window close be

From "Lew" <lew@THRWHITE.remove-dii-this>
Subject Re: Weird window close be
Message-ID <vZadnYmS7v1VNvvVnZ2dnUVZ_rGdnZ2d@comcast.com> (permalink)
Newsgroups comp.lang.java.gui
References <xaudnRiazPo3NfvVnZ2dneKdnZydnZ2d@bt.com>
Date 2011-04-27 15:46 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
RedGrittyBrick wrote:
> Doesn't the issue with partially constructed objects only arise if your 
> constructor passes a reference to itself to other methods?
> 
...
> Since the ancestral JComponent() constructor will have been completed 
> before BoxLayout() gets to make use of it - isn't it a fully constructed 
> JComponent so far as BoxLayout is concerned?

The questions you and Knute ask are valid and valuable.

I agree that there aren't dangers visible in the idiom of assembly in the 
constructor in the cases we usually see, where the constructing object sort of 
goes away after starting things off.

If I express a preference in the simple case it is that the notion of a run() 
on an argument to invokeLater() seems more "natural" than to run things from a 
constructor.  Indeed the recast does eliminate the need for an extra anonymous 
Runnable, making for cleaner code.

This I had not predicted, thinking /a priori/ that forcing code out of the 
constructor would increase complexity.  Instead, it simplified.

The second difference is one of planning.  Since the class is not written to 
prevent extension, it risks having subclasses.  Safety in the visible 
constructor might not convey to the inheritors.

Another danger is one of habit.  If one gets used to putting a lot of action 
in the constructor, one could trip on a corner case before realizing it.  If 
one habitually only lets fully-constructed objects participate, then safety is 
built in from the get-go.

Finally, it comes down to judgment and style.  When one knows that it is 
harmless, then of course there's no harm done.

Before reaching a decision on the style questions,

- should I rigorously push non-construction out of the constructor?
- how rigorously?

I wanted to explore how one could even do such a thing.  GUI assembly, so 
often and publicly done in constructors, seemed like a perfect candidate for 
the exploration.

I conclude that the rule to limit constructors to (object) construction is 
valuable, and can lead to lean code that expresses its algorithms as "literate 
code" quite naturally through method names and such.  It simplifies reasoning 
about thread boundaries in the cited example, and whether GUI assembly 
correctly resides on the EDT.  I found no disadvantages to the rule.  So far.

YMMV.

The group has responded intelligently with excellent and probing questions 
about the topic.  Your participation has immeasurably increased the value of 
the investigation, not least for forcing me to think more deeply about the 
issues involved.

-- 
Lew

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: Weird window close be "Lew" <lew@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000
  Re: Weird window close be "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000
    Re: Weird window close be "Lew" <lew@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000

csiph-web