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()"

 
 
Matija Papec
Guest
Posts: n/a
 
      10-07-2003
X-Ftn-To: Lack Mr G M

http://www.velocityreviews.com/forums/(E-Mail Removed) (Lack Mr G M) wrote:
> 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.


Isn't your last example potentially troublesome in case of class
inheritance?

--
Matija
 
Reply With Quote
 
 
 
 
Steve Grazzini
Guest
Posts: n/a
 
      10-07-2003
Matija Papec <(E-Mail Removed)> wrote:
>(E-Mail Removed) (Lack Mr G M) wrote:
>> my $tko1 = from_token MyToken('token' => $token);
>> my $tko2 = via_login MyToken('id' => yourid, 'password' => yourpassword);

>
> Isn't your last example potentially troublesome in case of class
> inheritance?


No.

It's troublesome if there are functions in the current package
called from_token() or via_login(). This has nothing to do with
constructors or inheritance -- it's a parsing issue. Indirect
object syntax is ugly and ambiguous and the parser doesn't always
get it right.

--
Steve
 
Reply With Quote
 
 
 
 
Iain Truskett
Guest
Posts: n/a
 
      10-08-2003
* Quantum Mechanic <(E-Mail Removed)>:

[...]
> 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?

[...]
> 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();


Or even:
my @classes = qw( Class Fnord Fnerk Xyzzy );
my $class = $classes[ rand @classes ];
my $new_instance = $class->new();



cheers,
--
Iain.
 
Reply With Quote
 
pkent
Guest
Posts: n/a
 
      10-09-2003
In article <2003Oct7.184133@ukwit01>, (E-Mail Removed) (Lack Mr G M)
wrote:

> Except that it is actually:
>
> $new_object = new Class;
>
> and you are *not* asking for a new class.


No, but imagine a better example that uses a slightly better name, like
where I have a class representing Employees. To create a new instance of
an employee I can say:

my $scott = new Employee( Password => 'tiger' );
or of course:
my $scott = Employee->new( Password => 'tiger' );

So in that less abstract example I'm not asking for a new _class_, I'm
asking for a new employee, i.e. a new thing of a certain kind. But at
least with perl you can choose the syntax that you prefer, or that makes
most sense in a given application (like the login tokens example you
give) - you probably can't have one hard and fast rule for every single
case.

Also the use of 'new' as a constructor is just a common convention, but
my constructor can be called anything:

my $scott = existing Employee( ID => '211' );

It's probably terrible to suggest that constructors need not be methods
at all, but could in fact be plain routines:

my $scott = Employee::existing( ID => '211' );
# $scott is an object

but you can do it - there are reasons not to do it

P

--
pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
Remove the tea to reply
 
Reply With Quote
 
Matija Papec
Guest
Posts: n/a
 
      10-09-2003
X-Ftn-To: Abigail

Abigail <(E-Mail Removed)> wrote:
><> which should have community consensus? Like perlstyle.pod which could make
><> some guidelines more official?
>
>Please no. *Personal* programming guidelines are ok. But let's not wade
>into the swamps of trying to derive guidelines with community consensus.
>Remember that are hundreds of thousands of Perl programmers, but only a
>few hundred of them are active on Perl forums - but there are dozens of
>those forums.


Ok, a whole community is too wide for such task, but actually I had in mind
smaller group of highly experienced programmers/teachers which can receive
blessings from Larry. Of course, if such guidelines threaten some basic
religious beliefs, they can never be done.

>style guide". If you want a language that comes with a notion on what
>is good style, and what should be avoided, you find it thataway --->
>www.python.org.


There is a difference between enforced standards and guidelines which you
use willingly. Also, freedom is a great thing but first you have to
//understand// implications of your programming decisions. If you're not
sure what you're doing, better grab some guide which will tell you how and
why (otherwise fully enjoy your freedom).
So, how much evil I am, is there some hope for me?



--
Matija
 
Reply With Quote
 
Iain Truskett
Guest
Posts: n/a
 
      10-10-2003
* Matija Papec <(E-Mail Removed)>:

[...]
> There is a difference between enforced standards and
> guidelines which you use willingly. Also, freedom is a
> great thing but first you have to //understand//
> implications of your programming decisions.


Perhaps you could draw up a list of guidelines, where for
every guideline you also have a counter-guideline or three.

Let people see what other people consider good practice and
pick and mix between them =)


cheers,
--
Iain.
 
Reply With Quote
 
Matija Papec
Guest
Posts: n/a
 
      10-10-2003
X-Ftn-To: Iain Truskett

Iain Truskett <(E-Mail Removed)> wrote:
>> great thing but first you have to //understand//
>> implications of your programming decisions.

>
>Perhaps you could draw up a list of guidelines, where for
>every guideline you also have a counter-guideline or three.
>
>Let people see what other people consider good practice and
>pick and mix between them =)


Ok, I'll write first commandment,

1) do "use strict;" in /all/ your programs until you learn from perldoc
strict. You are also free to continue such practice after this revelation.
(there is no counter-commandment for this one )



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