I only use assertion-like code in places where they're really really really needed. What an assertion says is that the developer of the function / method / package / object is smarter than any user of the function / method / package / object could ever be, and Knows What's Best (TM). Sometimes, that's true. Sometimes, however, it's not.

One developer on my team, many years ago, put assertions in his code all over the place to make sure we only did things His Way. When I started to try to use his code, I quickly ran into artificial limitations that if he had just left well alone, would work just fine. Things like passing in a lexical file handle that was pointing to a scalar wouldn't work (he checked that the handle was a GLOB!). After removing more than 70% of his assertions, the code continued to work fine, and started doing things he didn't even imagine as possible. But that's just because perl DWIMs very well.

So you may be thinking, "But, I know that, so I won't have a problem!" But the point is that you may know more about perl than this guy, but, like him, you may not know what it is that you don't know. He didn't know about lexical filehandles (he had been taught perl by a team that basically still used perl 4 syntax on perl 5.005). What is it that YOU don't know?

Even core modules aren't immune to this. In There be dragons (or, perl5.8's open stringref breaks...), I pointed out a problem with File::Copy. It isn't an assertion, but it IS code that makes an assumption about its input that just isn't necessarily valid. And when I copied (no pun intended) F::C's code and merely omitted the useless stat call, it worked fine.

Basically, in my experience, assertions are merely the writing down of assumptions, and not always the assumption that the author thinks they're assuming. And this goes ten times more for DWIMmery like Perl than for Java or C++ where polymorphism is more explicit.


In reply to Re: Your use of assertions in Perl ? by Tanktalus
in thread Your use of assertions in Perl ? by ady

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.