[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing optional parameters through a wrapper routine

In article <MPG.130a910d141f1260989a21@news.frii.com>, davidf@dfanning.com
(David Fanning) wrote:

> Having said that, it absolutely behooves you to check
> each and every variable you plan to use in your program
> to make sure you have a defined variable at the time
> you use it. This is normally done with the N_Elements
> function, since this function returns a 0 if its argument
> is undefined.

I admit to a certain conceptual confusion between undefined and optional
parameters.  If I omit an optional parameter, I have not defined it.  So
what's the difference between passing an undefined optional parameter and
omitting it?  Obviously, in practice the difference is in how you test for
whether the parameter is "there" or not.

> You would probably NOT use the odd syntax above for 
> the simple reason that it would fail if the number
> of parameters was 0 or 1. What you probably *would* do
> if p1 and p2 are required parameters and p3 is optional,
> is something like this:
>    IF N_Params() LT 2 THEN Message, 'Whoops, two parameters required'
>    IF N_Elements(p3) EQ 0 THEN p3 = p3DefaultValue

My syntax is not odd!  :-)  (Although it should be LT rather than EQ.)
If you use optional parameters, you need to test for each possible set of
optional parameters and the existence of mandatory parameters.

IF N_Params() LT 1 THEN Message, 'Whoops, p1 required'
IF (N_PARAMS() LT 2) OR (N_ELEMENTS(p2) EQ 0)) THEN do the default thing for p2
IF (N_PARAMS() LT 3) OR (N_ELEMENTS(p3) EQ 0)) THEN do the default thing for p3

This does not add any fundamental complexity to handling optional parameters.

Your earlier solution of setting all the optional parameters in the
calling requires me to figure out all of the default values for those
parameters.  This obviates the whole point of optional parameters
(convenient default values).  If optional parameters are there as a
convenience, make them convenient.

On to more productive topics ... :-)