Opinions on "new SomeObject" vs. "SomeObject->new()"

Discussion in 'Perl Misc' started by Patriote, Oct 6, 2003.

  1. Patriote

    Patriote Guest

    Some programmers use the style

    $objSome = new SomeObject;

    instead of

    $objSome = SomeObject->new();

    What are the penalties for using the former Java-like syntax if any?
    I've heard that it can cause problems with inheritance, but I've read
    no specifics.

    Thanks.
     
    Patriote, Oct 6, 2003
    #1
    1. Advertising

  2. Patriote

    Uri Guttman Guest

    >>>>> "P" == Patriote <> writes:

    P> Some programmers use the style
    P> $objSome = new SomeObject;

    P> instead of

    P> $objSome = SomeObject->new();

    P> What are the penalties for using the former Java-like syntax if any?
    P> I've heard that it can cause problems with inheritance, but I've read
    P> no specifics.

    read perldoc perlobj and look for the section on indirect object syntax.

    direct object calls are safer and a better style IMO.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Oct 6, 2003
    #2
    1. Advertising

  3. Patriote

    Tore Aursand Guest

    On Mon, 06 Oct 2003 14:36:33 -0700, Patriote wrote:
    > Some programmers use the style
    >
    > $objSome = new SomeObject;
    >
    > instead of
    >
    > $objSome = SomeObject->new();
    >
    > What are the penalties for using the former Java-like syntax if any?
    > I've heard that it can cause problems with inheritance, but I've read
    > no specifics.


    I prefer the latter, but as long as you design your classes to expect them
    to be used both ways, there shouldn't be any difference;

    sub new {
    my $proto = shift;
    my $class = ref( $proto ) || $proto;
    }


    --
    Tore Aursand <>
     
    Tore Aursand, Oct 7, 2003
    #3
  4. Patriote

    Uri Guttman Guest

    >>>>> "TA" == Tore Aursand <> writes:

    >> $objSome = new SomeObject;
    >> $objSome = SomeObject->new();


    TA> I prefer the latter, but as long as you design your classes to
    TA> expect them to be used both ways, there shouldn't be any
    TA> difference;

    TA> sub new {
    TA> my $proto = shift;
    TA> my $class = ref( $proto ) || $proto;
    TA> }

    the way a method is called has no semantic effect on the method
    itself. the method is always passed the object or class as the first
    argument. the difference between indirect (bad) and direct (good) method
    calls is a matter of syntax and possible issues with what sub gets
    called. see my other post for info on that.

    your code above has nothing to do with how the method is called. rather it
    allows it to be called with either an object or a classname.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Oct 7, 2003
    #4
  5. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    (Patriote) wrote in
    news::

    > Some programmers use the style
    >
    > $objSome = new SomeObject;
    >
    > instead of
    >
    > $objSome = SomeObject->new();
    >
    > What are the penalties for using the former Java-like syntax if any?
    > I've heard that it can cause problems with inheritance, but I've read
    > no specifics.


    I personally think it makes NO sense to create a new object from an
    existing one unless the new object is somehow a child or otherwise a
    descendant of the existing object. Thus, I never do:

    $new_object = $old_object->new();

    and I never code my new() methods to expect to be called from an existing
    object.

    For creating new objects, I think that

    $new_object = new Object;

    makes intuitive sense -- "give me a new Object". It reads like English.
    On the other hand,

    $new_object = Object->new();

    makes less sense to me, but at least I don't have to code differently for
    it. Users of my modules can use either syntax.

    - --
    Eric
    $_ = reverse sort $ /. r , qw p ekca lre uJ reh
    ts p , map $ _. $ " , qw e p h tona e and print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBP4ILUmPeouIeTNHoEQLyywCfTwMFSRzrRgkHfvj4/C2yHD3wF9oAoIew
    QOcbChXi6bJH8/rLpl9GRAl7
    =w42G
    -----END PGP SIGNATURE-----
     
    Eric J. Roode, Oct 7, 2003
    #5
  6. >>>>> "Patriote" == Patriote <> writes:

    Patriote> Some programmers use the style
    Patriote> $objSome = new SomeObject;

    Patriote> instead of

    Patriote> $objSome = SomeObject->new();

    Patriote> What are the penalties for using the former Java-like syntax if any?
    Patriote> I've heard that it can cause problems with inheritance, but I've read
    Patriote> no specifics.

    "Indirect Object" syntax can sometimes be ambiguous. Even Simon
    Cozens got bit by it <http://blog.simon-cozens.org/bryar.cgi/id_6416>.

    I can't recommend it for any normal code now.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
     
    Randal L. Schwartz, Oct 7, 2003
    #6
  7. >>>>> "Tore" == Tore Aursand <> writes:

    Tore> I prefer the latter, but as long as you design your classes to expect them
    Tore> to be used both ways, there shouldn't be any difference;

    Tore> sub new {
    Tore> my $proto = shift;
    Tore> my $class = ref( $proto ) || $proto;
    Tore> }

    Please, don't cargo-cult ref($proto) in this matter!
    See my rant on that at <http://www.perlmonks.org/index.pl?node_id=52089>.

    print "Just another Perl hacker,"

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
     
    Randal L. Schwartz, Oct 7, 2003
    #7
  8. Eric J. Roode <> wrote:
    > (Patriote) wrote in
    > news::
    >
    >> Some programmers use the style
    >>
    >> $objSome = new SomeObject;
    >>
    >> instead of
    >>
    >> $objSome = SomeObject->new();
    >>
    >> What are the penalties for using the former Java-like syntax if any?
    >> I've heard that it can cause problems with inheritance, but I've read
    >> no specifics.

    >
    > I personally think it makes NO sense to create a new object from an
    > existing one



    That code does not create a new object from an existing one.

    "SomeObject" is not an object (bad choice of name), it is a class.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 7, 2003
    #8
  9. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    (Tad McClellan) wrote in
    news::

    > Eric J. Roode <> wrote:
    >>
    >> I personally think it makes NO sense to create a new object from an
    >> existing one

    >
    >
    > That code does not create a new object from an existing one.
    >
    > "SomeObject" is not an object (bad choice of name), it is a class.
    >


    I understand that. I was making a general comment on creating objects.
    Some of the other people who responded to the OP went off on the tangent of
    creating a new object from an existing one, so I thought I'd chip my two
    cents in. Apologies if my wording was confusing.

    - --
    Eric
    $_ = reverse sort $ /. r , qw p ekca lre uJ reh
    ts p , map $ _. $ " , qw e p h tona e and print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBP4KmQWPeouIeTNHoEQKDawCgyjEuOVUgcyrGkBLCCMZM4upw+asAniH7
    PlIpWplGtyAK6XgRpXJD+0l5
    =O15d
    -----END PGP SIGNATURE-----
     
    Eric J. Roode, Oct 7, 2003
    #9
  10. Patriote

    Greg Bacon Guest

    In article <>,
    Randal L. Schwartz <> wrote:

    : >>>>> "Tore" == Tore Aursand <> writes:
    :
    : Tore> I prefer the latter, but as long as you design your classes to
    : Tore> expect them to be used both ways, there shouldn't be any
    : Tore> difference;
    :
    : Tore> sub new {
    : Tore> my $proto = shift;
    : Tore> my $class = ref( $proto ) || $proto;
    : Tore> }
    :
    : Please, don't cargo-cult ref($proto) in this matter!
    : See my rant on that at <http://www.perlmonks.org/index.pl?node_id=52089>.

    Here you argue in horribly obtuse terms: "If you want an instance method
    called 'new', I have no idea what it's doing." Bull! In the next two
    sentences, you give *two* ideas. If you not sure, read the pods, or use
    the source, Luke!

    What's worse, at least in the context of Perl, is that you're being
    rigidly dogmatic. If you don't like $obj->new, then don't use it!

    Greg
    --
    I also have to confess I was pro-"gun-control." In my defense, I was a
    juvenile and gun control is a juvenile notion.
    -- James Ostrowski
     
    Greg Bacon, Oct 7, 2003
    #10
  11. Patriote

    Anno Siegel Guest

    Greg Bacon <> wrote in comp.lang.perl.misc:
    > In article <>,
    > Randal L. Schwartz <> wrote:
    >
    > : >>>>> "Tore" == Tore Aursand <> writes:
    > :
    > : Tore> I prefer the latter, but as long as you design your classes to
    > : Tore> expect them to be used both ways, there shouldn't be any
    > : Tore> difference;
    > :
    > : Tore> sub new {
    > : Tore> my $proto = shift;
    > : Tore> my $class = ref( $proto ) || $proto;
    > : Tore> }
    > :
    > : Please, don't cargo-cult ref($proto) in this matter!
    > : See my rant on that at <http://www.perlmonks.org/index.pl?node_id=52089>.
    >
    > Here you argue in horribly obtuse terms: "If you want an instance method
    > called 'new', I have no idea what it's doing." Bull! In the next two
    > sentences, you give *two* ideas. If you not sure, read the pods, or use
    > the source, Luke!
    >
    > What's worse, at least in the context of Perl, is that you're being
    > rigidly dogmatic. If you don't like $obj->new, then don't use it!


    That's not the point. The "ref( $proto) || $proto" thing has become
    a sort of idiom that is, often mindlessly, squeezed into many new()
    methods, just because it's easy. In my opinion new() rarely needs
    to be callable as an object method, and to make it so routinely is
    wrong. Objecting a false idiom isn't dogmatism, it's a public
    service.

    Anno
     
    Anno Siegel, Oct 7, 2003
    #11
  12. Patriote

    Patriote Guest

    "Eric J. Roode" <> wrote in message news:<Xns940CD233996E9sdn.comcast@206.127.4.25>...
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > (Patriote) wrote in
    > news::
    >
    > > Some programmers use the style
    > >
    > > $objSome = new SomeObject;
    > >
    > > instead of
    > >
    > > $objSome = SomeObject->new();
    > >
    > > What are the penalties for using the former Java-like syntax if any?
    > > I've heard that it can cause problems with inheritance, but I've read
    > > no specifics.

    >
    > I personally think it makes NO sense to create a new object from an
    > existing one unless the new object is somehow a child or otherwise a
    > descendant of the existing object. Thus, I never do:
    >
    > $new_object = $old_object->new();
    >
    > and I never code my new() methods to expect to be called from an existing
    > object.
    >
    > For creating new objects, I think that
    >
    > $new_object = new Object;
    >
    > makes intuitive sense -- "give me a new Object". It reads like English.
    > On the other hand,
    >
    > $new_object = Object->new();
    >
    > makes less sense to me, but at least I don't have to code differently for
    > it. Users of my modules can use either syntax.
    >
    > - --
    > Eric
    > $_ = reverse sort $ /. r , qw p ekca lre uJ reh
    > ts p , map $ _. $ " , qw e p h tona e and print
    >
    > -----BEGIN PGP SIGNATURE-----
    > Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>
    >
    > iQA/AwUBP4ILUmPeouIeTNHoEQLyywCfTwMFSRzrRgkHfvj4/C2yHD3wF9oAoIew
    > QOcbChXi6bJH8/rLpl9GRAl7
    > =w42G
    > -----END PGP SIGNATURE-----



    Sorry, my nomenclature was stupid. When I said:

    $objSome = SomeObject->new();

    I meant:

    $objSome = SomeClass->new();
     
    Patriote, Oct 7, 2003
    #12
  13. >>>>> "Greg" == Greg Bacon <> writes:

    Greg> Here you argue in horribly obtuse terms: "If you want an instance method
    Greg> called 'new', I have no idea what it's doing." Bull! In the next two
    Greg> sentences, you give *two* ideas. If you not sure, read the pods, or use
    Greg> the source, Luke!

    I should have said "I have no idea which of the following two it is
    doing, or something else entirely." Does that help?

    Greg> What's worse, at least in the context of Perl, is that you're being
    Greg> rigidly dogmatic. If you don't like $obj->new, then don't use it!

    The problem is that your use of ->new is not likely to be confined to
    your cubicle. I don't speak for your private code. I speak for code
    that you're likely to upload to the CPAN to share with others.

    Don't make $instance->new. It obscures more than it communicates.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
     
    Randal L. Schwartz, Oct 7, 2003
    #13
  14. "Eric J. Roode" <> wrote in message news:<Xns940CD233996E9sdn.comcast@206.127.4.25>...
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > (Patriote) wrote in
    > news::
    >
    > > Some programmers use the style
    > >
    > > $objSome = new SomeObject;
    > >
    > > instead of
    > >
    > > $objSome = SomeObject->new();
    > >
    > > What are the penalties for using the former Java-like syntax if any?
    > > I've heard that it can cause problems with inheritance, but I've read
    > > no specifics.

    >
    > I personally think it makes NO sense to create a new object from an
    > existing one unless the new object is somehow a child or otherwise a
    > descendant of the existing object. Thus, I never do:
    >
    > $new_object = $old_object->new();
    >
    > and I never code my new() methods to expect to be called from an existing
    > object.
    >
    > For creating new objects, I think that
    >
    > $new_object = new Object;
    >
    > makes intuitive sense -- "give me a new Object". It reads like English.
    > On the other hand,
    >
    > $new_object = Object->new();
    >
    > makes less sense to me, but at least I don't have to code differently for
    > it. Users of my modules can use either syntax.


    Not to put too fine a point on it, but what is acceptable syntax for
    creating a new instance of a class of a given instance, when that
    class isn't known at the time of coding?

    In other words, if I know the class, I can do this:

    $new_instance = new Class;

    or

    $new_instance = Class->new();

    If I don't know the class ahead of time (determined at runtime, for
    instance), I'm looking at one of these:

    $new_instance = new $old_instance;

    $new_instance = $old_instance->new();

    $new_instance = (ref $old_instance)->new();

    The last one makes me wish for an alias to 'ref' called 'ClassOf'.
    Indeed, getting the 3rd ancestor class might be done like this:

    $new_great_great_uncle = (ClassOf($Johny,3))->new();

    for certain values of ClassOf.

    -QM
     
    Quantum Mechanic, Oct 7, 2003
    #14
  15. Patriote

    Matija Papec Guest

    X-Ftn-To: Randal L. Schwartz

    (Randal L. Schwartz) wrote:
    >Greg> What's worse, at least in the context of Perl, is that you're being
    >Greg> rigidly dogmatic. If you don't like $obj->new, then don't use it!
    >
    >The problem is that your use of ->new is not likely to be confined to
    >your cubicle. I don't speak for your private code. I speak for code
    >that you're likely to upload to the CPAN to share with others.
    >
    >Don't make $instance->new. It obscures more than it communicates.


    I think you really should explain that in foreword of Damian book. :)



    --
    Matija
     
    Matija Papec, Oct 7, 2003
    #15
  16. >>>>> "Matija" == Matija Papec <> writes:

    >> Don't make $instance->new. It obscures more than it communicates.


    Matija> I think you really should explain that in foreword of Damian book. :)

    I make the point in the Alpaca. Is that enough?

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
     
    Randal L. Schwartz, Oct 7, 2003
    #16
  17. >>>>> "Kevin" == Kevin Michael Vail <> writes:

    Kevin> Except that this doesn't work very well with multiple inheritance.

    Some would claim that "multiple inheritance" is itself to blame.

    :)

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
     
    Randal L. Schwartz, Oct 7, 2003
    #17
  18. Patriote

    Greg Bacon Guest

    In article <>,
    Randal L. Schwartz <> wrote:

    : >>>>> "Greg" == Greg Bacon <> writes:
    :
    : Greg> Here you argue in horribly obtuse terms: "If you want an
    : Greg> instance method called 'new', I have no idea what it's doing."
    : Greg> Bull! In the next two sentences, you give *two* ideas. If you
    : Greg> not sure, read the pods, or use the source, Luke!
    :
    : I should have said "I have no idea which of the following two it is
    : doing, or something else entirely." Does that help?

    No, because your argument is still obtuse. This is Perl: you have the
    message's surrounding context, you have the pods, and you have the
    source. You must be at least this tall to ride.

    : Greg> What's worse, at least in the context of Perl, is that you're
    : Greg> being rigidly dogmatic. If you don't like $obj->new, then don't
    : Greg> use it!
    :
    : The problem is that your use of ->new is not likely to be confined to
    : your cubicle. I don't speak for your private code. I speak for code
    : that you're likely to upload to the CPAN to share with others.

    Now you're being paternalistic and tilting at windmills. Yes, we
    could all put together contrived examples that are problematic in
    this respect, but only if the subject programmer can't be bothered
    to look at the pods or source.

    Greg
    --
    Democracy becomes a government of bullies tempered by editors.
    -- Ralph Waldo Emerson
     
    Greg Bacon, Oct 7, 2003
    #18
  19. Patriote

    Lack Mr G M Guest

    In various articles someone or other wrote:
    |>
    |> For creating new objects, I think that
    |>
    |> $new_object = new Object;
    |>
    |> makes intuitive sense -- "give me a new Object".

    Except that it is actually:

    $new_object = new Class;

    and you are *not* asking for a new class.

    |> $new_object = Object->new();
    |>
    |> makes less sense to me

    Similarly, this is really:

    $new_object = Class->new();

    and you are asking the Class to implement a new(). So for me this
    latter one makes more sense, in English.

    |> Don't make $instance->new. It obscures more than it communicates.

    That's what documentation is for. Using something without first
    reading the instructions is Not a Good Idea.


    I have a class which has 2 constructors and *neither* of then is
    called new. It's a security token module which can either be created by
    internally calling an issuing server (with id+pwd) or from a
    previously-obtained token.

    So, my code looks like:

    my $tko1 = MyToken->from_token('token' => $token);
    my $tko2 = MyToken->via_login('id' => yourid, 'password' => yourpassword);

    Some poeple seem to be advocating this conveying more:

    my $tko1 = from_token MyToken('token' => $token);
    my $tko2 = via_login MyToken('id' => yourid, 'password' => yourpassword);

    I can't see it myself.


    --
    --------- Gordon Lack --------------- ------------
    This message *may* reflect my personal opinion. It is *not* intended
    to reflect those of my employer, or anyone else.
     
    Lack Mr G M, Oct 7, 2003
    #19
  20. Patriote

    Matija Papec Guest

    X-Ftn-To: Randal L. Schwartz

    (Randal L. Schwartz) wrote:
    >>> Don't make $instance->new. It obscures more than it communicates.

    >
    >Matija> I think you really should explain that in foreword of Damian book. :)
    >
    >I make the point in the Alpaca. Is that enough?


    I guess, btw where is your sense of humor, notice smiley above.

    As for cargo cult programming, how about some general programming guidelines
    which should have community consensus? Like perlstyle.pod which could make
    some guidelines more official?



    --
    Matija
     
    Matija Papec, Oct 7, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: another request for OPINIONS

    Kevin Spencer, Jul 7, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    351
    msnews.microsoft.com
    Jul 14, 2003
  2. Utter Newbie
    Replies:
    0
    Views:
    474
    Utter Newbie
    Jul 28, 2003
  3. JPerrin

    Opinions of Hostdepartment

    JPerrin, Oct 28, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    346
    JPerrin
    Oct 28, 2003
  4. JoeBlack
    Replies:
    2
    Views:
    367
    JoeBlack
    Oct 4, 2004
  5. Wysiwyg
    Replies:
    2
    Views:
    506
    Wysiwyg
    Dec 27, 2004
Loading...

Share This Page