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?? |
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 Section
Meditations
|
|