http://www.perlmonks.org?node_id=765746


in reply to Re^3: Preferred technique for named subroutine parameters?
in thread Preferred technique for named subroutine parameters?

You have to test that you have the correct number of arguments anyway (in most cases, at least).

Also: specific error messages are way better than generic ones.

  • Comment on Re^4: Preferred technique for named subroutine parameters?

Replies are listed 'Best First'.
Re^5: Preferred technique for named subroutine parameters?
by AnomalousMonk (Archbishop) on May 22, 2009 at 22:16 UTC
    You have to test that you have the correct number of arguments anyway (in most cases, at least).
    But this is really about parameter validation: required versus optional parameters, acceptable ranges of parameter values, misspelled parameter names, etc. Depending on the situation, validation can be quite simple (or non-existent!) or quite involved. In any case, it is something that can only be handled 'within the function'.

    But again, to me, the question of whether each named parameter has some value associated with it is a question that can be (and is best) handled by default, right there and then at the function call.

    Also: specific error messages are way better than generic ones.
    I tend to agree, but this becomes a matter of individual taste. For myself, a message like  Odd number of elements in anonymous hash at 765727_1.pl line 38. is both sufficiently specific and sufficiently proximate to the point of occurrence – especially if the program died at line 38!