Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Opinions on "new SomeObject" vs. "SomeObject->new()" (http://www.velocityreviews.com/forums/t882970-opinions-on-new-someobject-vs-someobject-new.html)

Patriote 10-06-2003 09:36 PM

Opinions on "new SomeObject" vs. "SomeObject->new()"
 
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.

Uri Guttman 10-06-2003 10:01 PM

Re: Opinions on "new SomeObject" vs. "SomeObject->new()"
 
>>>>> "P" == Patriote <patriote@fastmail.fm> 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 ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Tore Aursand 10-06-2003 11:01 PM

Re: Opinions on "new SomeObject" vs. "SomeObject->new()"
 
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.no>

Uri Guttman 10-07-2003 12:39 AM

Re: Opinions on "new SomeObject" vs. "SomeObject->new()"
 
>>>>> "TA" == Tore Aursand <tore@aursand.no> 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 ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Eric J. Roode 10-07-2003 12:39 AM

Re: Opinions on "new SomeObject" vs. "SomeObject->new()"
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

patriote@fastmail.fm (Patriote) wrote in
news:dced8a08.0310061336.35f80601@posting.google.c 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-----

Randal L. Schwartz 10-07-2003 12:57 AM

Re: Opinions on "new SomeObject" vs. "SomeObject->new()"
 
>>>>> "Patriote" == Patriote <patriote@fastmail.fm> 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
<merlyn@stonehenge.com> <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 10-07-2003 01:03 AM

Re: Opinions on "new SomeObject" vs. "SomeObject->new()"
 
>>>>> "Tore" == Tore Aursand <tore@aursand.no> 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
<merlyn@stonehenge.com> <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!

Tad McClellan 10-07-2003 05:21 AM

Re: Opinions on "new SomeObject" vs. "SomeObject->new()"
 
Eric J. Roode <REMOVEsdnCAPS@comcast.net> wrote:
> patriote@fastmail.fm (Patriote) wrote in
> news:dced8a08.0310061336.35f80601@posting.google.c 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



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
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Eric J. Roode 10-07-2003 11:41 AM

Re: Opinions on "new SomeObject" vs. "SomeObject->new()"
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

tadmc@augustmail.com (Tad McClellan) wrote in
news:slrnbo4j9s.hrf.tadmc@magna.augustmail.com:

> Eric J. Roode <REMOVEsdnCAPS@comcast.net> 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/AwUBP4KmQWPeouIeTNHoEQKDawCgyjEuOVUgcyrGkBLCCMZM4u pw+asAniH7
PlIpWplGtyAK6XgRpXJD+0l5
=O15d
-----END PGP SIGNATURE-----

Greg Bacon 10-07-2003 12:06 PM

Re: Opinions on "new SomeObject" vs. "SomeObject->new()"
 
In article <642aea591ecc43a4da321e6220dbeac1@news.teranews.co m>,
Randal L. Schwartz <merlyn@stonehenge.com> wrote:

: >>>>> "Tore" == Tore Aursand <tore@aursand.no> 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


All times are GMT. The time now is 02:14 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57