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


Groups > comp.lang.java.programmer > #34473

Re: Are you sure, Mr. Gosling?

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Are you sure, Mr. Gosling?
Date 2016-06-04 16:43 -0400
Organization A noiseless patient Spider
Message-ID <nivehs$9e0$1@dont-email.me> (permalink)
References <Gosling-20160604185910@ram.dialup.fu-berlin.de> <niva2t$13sk$1@gioia.aioe.org>

Show all headers | View raw


On 6/4/2016 3:27 PM, Arne Vajhøj wrote:
> On 6/4/2016 2:01 PM, Stefan Ram wrote:
>>   There is this famous book »The Java Programming Language,
>>   Fourth Edition« by James Gosling et al.
>>
>>   In it, it is written about the »idiom of copy constructor«:
>>
>>       »This idiom is not used much within the Java class
>>       libraries, because the preferred way to make a direct
>>       copy of an object is by using the clone method.«
>>
>>   Are you sure about this, Mr. Gosling?

     Why ask here?  Do you think he reads this newsgroup?

> I do not remember ever to have written a class with a constructor
> with a single argument of same type.

     I've done it, but not very often.  Usually it's when the class is
mutable (for what seem to be good reasons) but I need an instance that
won't be mutated:

	class MyClass {

	    private final List<Thing> things = new ArrayList<>();

	    public void rememberThing(Thing thing) {
	        // Don't let the Thing change after I've remembered it.
	        things.add(new Thing(thing));
	    }

	    public Thing getThingAtIndex(int index) {
	        // Don't expose a remembered Thing to possible change.
	        return new Thing(things.get(index));
	    }

	    ...
	}

> And the only example that I can remember having seen
> is java.lang.String. And usage of that is usually a code
> smell.

     Well, the JavaDoc suggests that any class implementing Collection
ought to provide a constructor that takes a Collection argument.  So
we could *almost* say that ArrayList has a constructor that takes a
(slightly disguised) ArrayList as its argument.  Similar remarks apply
to Map.

     FWIW, Joshua Bloch appears to disagree with Gosling et al.  From
"Effective Java:"

	Is all this complexity [of implementing Cloneable] really
	necessary?  Rarely.  [...]  <b>you are probably better off
	providing some other means of object copying or simply not
	providing the capability.</b> [emphasis his]

	<b>A fine approach to object copying is to provide a <i>copy
	constructor</i>. [...]</b> [emphasis and italics his]

	The copy constructor approach and its static factory variant
	have many advantages over Cloneable/clone [...]

     So: Who ya gonna trust?  The guy who thought unsigned integers
were "too complex" or the guy who invented collections?

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-04 15:27 -0400
  Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-04 16:43 -0400
    Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-04 18:44 -0400
      Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-05 08:05 -0400
        Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-05 08:31 -0400
        Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-06 05:22 -0700
          Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-06 09:00 -0400
            Re: Are you sure, Mr. Gosling? v_borchert@despammed.com (Volker Borchert) - 2016-06-06 16:06 +0000
              Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-06 22:56 +0200
                Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-07 05:08 -0700
                Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-07 08:41 -0400
                Re: Are you sure, Mr. Gosling? Martin Gregorie <martin@address-in-sig.invalid> - 2016-06-07 13:14 +0000
                Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-07 06:30 -0700
                Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-07 19:10 +0200
                Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-07 10:22 -0700
                Re: Are you sure, Mr. Gosling? Mike Amling <mamling@chaff.us> - 2016-06-07 15:31 -0500
                Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-08 06:31 -0700
                Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-08 10:41 -0400
                Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-08 10:21 -0700
                Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-08 14:10 -0400
                Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-08 20:54 +0200
                Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-08 16:36 -0400
                Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-08 23:11 +0200
                Re: Are you sure, Mr. Gosling? Lew <lewbloch@gmail.com> - 2016-06-10 12:20 -0700
                Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-14 19:10 +0200
                Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-07 17:12 -0400
                Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-07 23:25 +0200
                Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-07 17:33 -0400
          Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-06 21:18 -0400

csiph-web