prototypes - use or not?

D

Dan Becker

Perl Best Practices (Conway), section 9.10, has a pretty clear statement
that subroutine prototypes should not be used. Period. (It says that the
disadvantages generally outweigh the limited advantages.) But it's still in
the language. So I'm confused. Is this generally accepted advice? or should
it be applied only in specific situations? or, generally speaking, avoided
in specific situations?

Dan Becker
 
U

Uri Guttman

DB> Perl Best Practices (Conway), section 9.10, has a pretty clear
DB> statement that subroutine prototypes should not be
DB> used. Period. (It says that the disadvantages generally outweigh
DB> the limited advantages.) But it's still in the language. So I'm
DB> confused. Is this generally accepted advice? or should it be
DB> applied only in specific situations? or, generally speaking,
DB> avoided in specific situations?

just because PBP doesn't like something doesn't mean it will be removed
from perl5. about the only think i know that has ever been removed was
pseudo-hashes and they were very bad and evil. prototypes have a few
real uses but almost none for the average perl hacker. so the point is
just what pbp says, don't use them unless you have a very good reason to
use them. if you can't figure that out, then you don't know enough to
use them. among other dumb things newbies do is use prototypes on
methods where they have no effect at all.

uri
 
J

John W. Krahn

Dan said:
Perl Best Practices (Conway), section 9.10, has a pretty clear statement
that subroutine prototypes should not be used. Period. (It says that the
disadvantages generally outweigh the limited advantages.) But it's still in
the language. So I'm confused. Is this generally accepted advice? or should
it be applied only in specific situations? or, generally speaking, avoided
in specific situations?

See also Tom Christiansen's article "Far More Than Everything You've Ever
Wanted to Know about Prototypes in Perl" at:

http://library.n0i.net/programming/perl/articles/fm_prototypes/



John
 
X

Xiong Changnian

Dan Becker said:
Perl Best Practices (Conway), section 9.10, has a pretty clear statement
that subroutine prototypes should not be used. Period.

The trouble with prototypes, as I see it, is that they may not do what
you expect -- depending, of course, on what you expect. Prototypes don't
do type checking; rather, they force types. I think it might be better
to write your code so as to supply the right type of param in the first
place.

In most cases, I believe, if you're thinking about prototypes, you'd be
better off checking params explicitly and writing good docs.

Prototypes doubtless have their uses and if you stick with this thread
long enough, you'll find an avid supporter. I'm not going to get into a
war over them. I do suggest that before you decide to use them, you
spend quite a bit of time learning exactly what they do and do not do.
 
G

gf

Dan said:
Perl Best Practices (Conway), section 9.10, has a pretty clear statement
that subroutine prototypes should not be used. Period. (It says that the
disadvantages generally outweigh the limited advantages.) But it's still in
the language. So I'm confused. Is this generally accepted advice? or should
it be applied only in specific situations? or, generally speaking, avoided
in specific situations?

Conway says it's usually not a good idea but gives examples when you
might want to. It's up to you to decide whether the prototypes make
sense or not in a particular script.

Personally, I don't use them. I write in checks when I enter a
subroutine to see if I've gotten sensible values and/or types for the
parameters.

I also stopped using positional parameters in subroutines and instead
follow Conway's recommendation to use hashes or hash-refs. Doing that
opens up some interesting possibilities for using defaults for values
not passed in, or passing in a set of defaults and overriding a
particular value. It makes for more typing as I write the code
initially, but I think my reuse goes up over time.
 
M

Michael Carman

I also stopped using positional parameters in subroutines and instead follow
Conway's recommendation to use hashes or hash-refs. [...] It makes for more
typing as I write the code initially, but I think my reuse goes up over time.

It also makes it a lot easier to read/debug when you can tell what's what from
the call. :D That said I find that functions with <= 3 parameters are usually
clearer with positional notation.

-mjc
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top