Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Here is an answer to your rhetorical question.

The problem with using prototypes is that you are making assumptions for the person using your code about exactly how they are going to use it. Prototypes have many silent, nasty and unexpected side-effects. For instance perhaps the person using your code wants to have the parameters in an array. Oops, they need an array slice, but may not understand why. Perhaps they want to dynamically decide what goes into your second argument and so use a trinary operator. Oops, not allowed by the prototype, you need to use temporaries. Perhaps they are returning the output of a function to decide what your parameters should be. Oops, not allowed by the prototype, you need a temporary array and then a slice. Either that or multiple temporary scalars.

None of these are very intuitive bugs for me to figure out. Nor when I have to bring someone up to speed on Perl do I want to waste energy on explaining the whys and wherefores of prototype-caused errors. It is simpler to just pretend that they don't exist.

So using them, even when the usage is clear, is problematic at best. It is, in fact, very easy to achieve the same basic effect without the side-effects by a run-time check inside of the function. And the same approach will also work inside of method calls. It works no matter what order in your file the functions are declared in. Consistency and flexibility are Good. Making assumptions for your caller is Bad.

But there are more reasons not to use prototypes for checking. When you use prototypes, you are forcing yourself into a single positional style of programming. You have put on a blinker where you are disregarding the possibilities of list-based constructs, or the use of named parameter processing. But I find that using positional logic is inherently buggy (people make mistakes with what order parameters go in), and I find tremendous flexiblity in using dynamic list-based approaches where applicable.

So I find the win from prototypes to come with problems at best, and at worst trying to get it limits the kinds of solutions that can be tried. In other words they get me coming and going.

All of this is why when this topic came up what I said in chatter was, Given the choice, I would refuse to work with a programmer who understood how Perl's prototypes work, understood the gotchas, and used them liberally anyways. It would be a cognitive mismatch. What I mean by that is that working with that code would be a constant source of frustration for me. And I simply would not want to do it.


In reply to Re (tilly) 2: When to use Prototypes? by tilly
in thread When to use Prototypes? by demerphq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (6)
    As of 2014-09-21 09:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (168 votes), past polls