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


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

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-07 08:41 -0400
Organization A noiseless patient Spider
Message-ID <nj6fep$7je$1@dont-email.me> (permalink)
References (5 earlier) <1eb77568-ae31-4316-ac21-a33db79c5aa4@googlegroups.com> <nj3s6h$l08$1@dont-email.me> <nj471k$f77$2@Gaia.teknon.de> <drm6bcFhke1U1@mid.individual.net> <5c864ac4-9634-4c88-9d6b-d14dbf3b3886@googlegroups.com>

Show all headers | View raw


On 6/7/2016 8:08 AM, Eric Douglas wrote:
> On Monday, June 6, 2016 at 4:56:24 PM UTC-4, Robert Klemme wrote:
>> The infamous "copy constructor" usually has a single parameter of the
>> same type (sometimes superclass or interface) but will also happily
>> accept any subclass instance of the argument type.  If implemented with
>> "copy" logic the best this will do is copy the state of the argument
>> that is relevant to this class.  It is not guaranteed to create a copy
>> of the other instance.  The central point of this type of object
>> creation is that calling code is in control of the resulting object's type.
>
> So the copy constructor is preferable?  I aim to get a custom class to copy itself, and I want a class extending that class to copy itself..

     *I* prefer copy constructors over Cloneable implementations, but not
everyone does.  As has already been pointed out, Experts Disagree.

     Here's something to keep in mind, though: Copying something seldom
"advances the computation" very much.  It doesn't create new information
(not very much, anyhow), so the time and memory spent copying doesn't
"add value" (much).  Why, then, do we want to copy?  I can think of
three reasons; perhaps you can think of others:

     - We need "defensive copies" of mutable objects.  It seems to me
       that this is a weakness of Java, but it's a weakness that's
       not going away, so ...

     - We don't really want "copies," but "objects in a known initial
       state" that we then intend to modify independently.  Factory
       methods (or better-parameterized constructors) might serve as
       alternatives to copying.

     - We actually need distinct instances of objects in identical
       state, like `new String("hello")' or `new Integer(42)'.  This
       strikes me as a possible but unlikely scenario, one that ought
       to raise an eyebrow and perhaps prompt a redesign.  (Someone
       else called this a "code smell.")

     In short, although I happen to prefer copy constructors, I write
them only rarely.  Copying, however you do it, is if not a code smell
then at least a code whiff.

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