Wally Hartshorn has asked for the wisdom of the Perl Monks concerning the following question:

In looking through Tom Christiansen's Perl Style guide recently, under "Defensive Programming" was an item that said simply "Parameter asserts". No explanation was given as to what "parameter asserts" are and how they are to be used.

I'm making a wild guess that the phrase somehow means "functions should check parameters passed to them to verify that they are reasonable". Does that sound correct?

Two questions:

Wally Hartshorn

(Plug: Visit JavaJunkies, PerlMonks for Java)

  • Comment on Parameter Asserts - How do you use them?

Replies are listed 'Best First'.
Re: Parameter Asserts - How do you use them?
by adrianh (Chancellor) on Apr 16, 2003 at 21:16 UTC
    How would I code "parameter asserts"? I checked the camel book, but there doesn't seem to be an "assert" keyword.

    Depending on mood I may do something like this:

    sub new { my ($self, %param) = @_; my ($dbh, $foo) = map { $param($_) || croak "need $_" } qw(dbh foo +); # ... stuff that uses $dbh and $foo ... }

    or use a module like Params::Validate or Carp::Assert.

    Do you personally use them in your code? Why or why not?

    Yes, I tend to use them. For two reasons:

    1. They make design decisions explicit in the code. Saying:

      croak "need a foo" unless UNIVERSAL::isa($foo, 'Foo');

      says a lot about your code's contracts and, unlike paper documents or UML diagrams, won't get out of sync with the code.

    2. It makes debugging easier. You get a specific error message near the place where the "bad" data went in (as opposed to a possibly less specific error message where the "bad" data gets used).

    Reasons not to use them:

    Efficiency. Assertions take time and in a tight loop or in a method that is called many times this can add up. You can use constants to allow you to switch your assertions on/off at compile time (the documentation for Carp::Assert has some examples of this.) There are some indications that there will be "real" assertions in the next perl5 that can be switched on/off without a compile time penalty.

    When it's obvious that they're not needed (for some definition of "obvious" :-). For example, in:

    package Foo; use Carp; use Regexp::Common; # ... rest of class ... sub add { my ($self, $n) = @_; croak "need Foo object" unless UNIVERSAL::isa($self, 'Foo'); croak "need number" unless $n =~ $RE{num}{real} $self->{total} += $n; };

    the two assertions are redundant (IMHO). If $self isn't a Foo object then somebody is calling my method as a subroutine and deserves all that they get. If $n isn't a number then the addition is going to throw a warning anyway.

Re: Parameter Asserts - How do you use them?
by demerphq (Chancellor) on Apr 16, 2003 at 22:04 UTC

      assertion

      1. \As*ser"tion\, n. [L. assertio, fr. asserere.] The act of asserting, or that which is asserted; positive declaration or averment; affirmation; statement asserted; position advanced.
      2. <programming> An expression which, if false, indicates an error. Assertions are used for debugging by catching can't happen errors.
      3. <programming> In logic programming, a new fact or rule added to the database by the program at run time. This is an extralogical or impure feature of logic programming languages.

      Dictionary.com

    Id just like to note that in newer versions of perl (5.10) there will be a much more extensive and powerful assertion/debugging interface than was available in earlier versions. You will be able to use a switch to compile time remove all asssertion code, as well as a number of other features. Carp::Assert will at that time become obsolete, or perhaps a wrapper for cross version compatibility.

    BTW: whenever you say something like

    sub do_something { my $with_x=shift or die "Must have an x to do_something with!"; }

    you are doing an assertion.


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
Re: Parameter Asserts - How do you use them?
by dze27 (Pilgrim) on Apr 16, 2003 at 20:08 UTC

    I think, as you suggest, that he is just talking about doing a sanity check on the input to a function. There is a module, Carp::Assert on CPAN that you might find useful for doing assertions.

Re: Parameter Asserts - How do you use them?
by perrin (Chancellor) on Apr 16, 2003 at 21:05 UTC