prototypes - use or not?

Discussion in 'Perl Misc' started by Dan Becker, Mar 27, 2007.

  1. Dan Becker

    Dan Becker Guest

    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
    Dan Becker, Mar 27, 2007
    #1
    1. Advertising

  2. Dan Becker

    Uri Guttman Guest

    >>>>> "DB" == Dan Becker <> writes:

    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Mar 27, 2007
    #2
    1. Advertising

  3. Dan Becker wrote:
    > 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
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, Mar 27, 2007
    #3
  4. In article <46098403$>,
    "Dan Becker" <> wrote:

    > 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.
    --
    Xiong Changnian
    xiong102ATxuefangDOTcom

    --
    Posted via a free Usenet account from http://www.teranews.com
    Xiong Changnian, Mar 31, 2007
    #4
  5. Dan Becker

    gf Guest

    Dan Becker wrote:
    > 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.
    gf, Apr 9, 2007
    #5
  6. On 4/9/2007 4:08 PM, gf wrote:
    >
    > 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
    Michael Carman, Apr 10, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lars Hedström

    Prototypes of html-pages

    Lars Hedström, Apr 8, 2004, in forum: HTML
    Replies:
    3
    Views:
    382
    Jim Roberts
    Apr 8, 2004
  2. Paul Davis
    Replies:
    2
    Views:
    309
    Paul Davis
    Jul 15, 2003
  3. Dave Vandervies

    Default arguments and prototypes

    Dave Vandervies, Oct 2, 2003, in forum: C++
    Replies:
    5
    Views:
    467
    Victor Bazarov
    Oct 3, 2003
  4. pemo

    Scope/use of prototypes Qs

    pemo, Mar 29, 2006, in forum: C Programming
    Replies:
    9
    Views:
    271
    Michael Wojcik
    Mar 31, 2006
  5. Replies:
    1
    Views:
    522
    Keith Thompson
    May 14, 2009
Loading...

Share This Page