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

P

Patriote

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

Uri Guttman

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
 
T

Tore Aursand

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;
}
 
U

Uri Guttman

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
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) (Patriote) wrote in
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-----
 
R

Randal L. Schwartz

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

Randal L. Schwartz

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,"
 
T

Tad McClellan

Eric J. Roode said:
(e-mail address removed) (Patriote) wrote in


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

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) (Tad McClellan) wrote in
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-----
 
G

Greg Bacon

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

Anno Siegel

Greg Bacon said:
: 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
 
P

Patriote

Eric J. Roode said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) (Patriote) wrote in


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();
 
R

Randal L. Schwartz

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

Quantum Mechanic

Eric J. Roode said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) (Patriote) wrote in


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
 
M

Matija Papec

X-Ftn-To: Randal L. Schwartz

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. :)
 
R

Randal L. Schwartz

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

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

Randal L. Schwartz

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

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

:)
 
G

Greg Bacon

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

Lack Mr G M

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

Matija Papec

X-Ftn-To: Randal L. Schwartz

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top