Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Your use of assertions in Perl ?

by Tanktalus (Canon)
on Sep 08, 2008 at 14:51 UTC ( [id://709783]=note: print w/replies, xml ) Need Help??


in reply to Your use of assertions in Perl ?

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.

Replies are listed 'Best First'.
Re^2: Your use of assertions in Perl ?
by SuicideJunkie (Vicar) on Sep 18, 2008 at 19:21 UTC

    I can't agree more.

    There have been so many times where I have been stuck sitting there knowing that what I want to do will work, but not being allowed to do it.

    For a concrete example: a monitoring program which sends alerts when your server's disk starts to get full at $work. I want just a "warning" at 100% full, not a "critical", since the disk in question is very low priority and I don't want the system to cry "Wolf!" The obvious solution is to set the critical level to 101% full, and therefore it will never trigger, leaving 100% for the warning.
    Naturally somebody went and made the GUI only allow 0-100% to be entered, and also require something in the critical field. Yet, if you snoop around in the database and poke 101 into the correct cell, it all works perfectly as intended. Unfortunately you can't do workarounds like that and still expect corporate support for it.

    ...Anyways... I would just like to say to all the assertive coders:
    Please do not assert that the input is within an expected range.
    Instead, assert to protect against things which will make your function fail.

    Note that this is the opposite of taint checking; instead of only allowing known-good data from untrusted users, your users are now the authors useing your module. They are fully trusted (and can edit your code if they really want to), so you want to maintain the maximum flexibility while providing some convenient notification when they're about to burn themselves.

    The distinction here is the difference between a guard rail on the highway and the padded rubber room your power users will require after trying to use your code. :)


    PS: this, of course, only applies to modules that do not interact with untrusted users. If taint checking is a good idea for your module, then highly restrictive assertions are probably a good idea too.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://709783]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-18 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found