Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Opinions on "new SomeObject" vs. "SomeObject->new()"

Reply
Thread Tools

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

 
 
Anno Siegel
Guest
Posts: n/a
 
      10-07-2003
Greg Bacon <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> In article <(E-Mail Removed) m>,
> Randal L. Schwartz <(E-Mail Removed)> wrote:
>
> : >>>>> "Tore" == Tore Aursand <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Patriote
Guest
Posts: n/a
 
      10-07-2003
"Eric J. Roode" <(E-Mail Removed)> wrote in message news:<Xns940CD233996E9sdn.comcast@206.127.4.25>...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> http://www.velocityreviews.com/forums/(E-Mail Removed) (Patriote) wrote in
> news:(E-Mail Removed) om:
>
> > 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();
 
Reply With Quote
 
 
 
 
Randal L. Schwartz
Guest
Posts: n/a
 
      10-07-2003
>>>>> "Greg" == Greg Bacon <(E-Mail Removed)> 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
<(E-Mail Removed)> <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!
 
Reply With Quote
 
Quantum Mechanic
Guest
Posts: n/a
 
      10-07-2003
"Eric J. Roode" <(E-Mail Removed)> wrote in message news:<Xns940CD233996E9sdn.comcast@206.127.4.25>...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> (E-Mail Removed) (Patriote) wrote in
> news:(E-Mail Removed) om:
>
> > 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
 
Reply With Quote
 
Matija Papec
Guest
Posts: n/a
 
      10-07-2003
X-Ftn-To: Randal L. Schwartz

(E-Mail Removed) (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
 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      10-07-2003
>>>>> "Matija" == Matija Papec <(E-Mail Removed)> 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
<(E-Mail Removed)> <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!
 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      10-07-2003
>>>>> "Kevin" == Kevin Michael Vail <(E-Mail Removed)> 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
<(E-Mail Removed)> <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!
 
Reply With Quote
 
Greg Bacon
Guest
Posts: n/a
 
      10-07-2003
In article <(E-Mail Removed) m>,
Randal L. Schwartz <(E-Mail Removed)> wrote:

: >>>>> "Greg" == Greg Bacon <(E-Mail Removed)> 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
 
Reply With Quote
 
Lack Mr G M
Guest
Posts: n/a
 
      10-07-2003
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 --------------- (E-Mail Removed) ------------
This message *may* reflect my personal opinion. It is *not* intended
to reflect those of my employer, or anyone else.
 
Reply With Quote
 
Matija Papec
Guest
Posts: n/a
 
      10-07-2003
X-Ftn-To: Randal L. Schwartz

(E-Mail Removed) (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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HEXUS.opinions :: Have a happy happy gaming holiday Silverstrand Front Page News 0 12-23-2005 04:12 PM
HEXUS.opinions :: Hey, take a look at my 15-inch $2,300 TV Silverstrand Front Page News 0 09-20-2005 03:34 AM
OPINIONS/EXPERIENCE PLEASE: Wireless Network Adapters and Base Stations of different brands Jimmy Dean Wireless Networking 2 02-22-2005 02:42 PM
Need some advice and opinions... MJBrown Microsoft Certification 4 11-06-2003 05:03 PM
Test-King..Your Opinions Scott Microsoft Certification 2 10-30-2003 08:03 PM



Advertisments