Re: Passing zero as a Parameter/ NOT KEYWORD_SET [message #16045 is a reply to message #16040] |
Wed, 30 June 1999 00:00   |
Vapuser
Messages: 63 Registered: November 1998
|
Member |
|
|
Martin Schultz <mgs@io.harvard.edu> writes:
> J.D. Smith wrote:
>>
>>
>> That's a bit dangerous. [...]
> Indeed ;-)
>>
>> The best way to proceed is pretend keyword_set() was really
>> named is_defined_and_non_zero(). Forget that it's called
>> keyword_set().
> In fact it is "is_defined_and_uneven" !
HuH?!
PRO junk,b=b
print,keyword_set(b)
END
IDL> junk
0
IDL> junk,b=1
1
IDL> junk,/b
1
IDL> junk,b=0
0
IDL> junk,b=2
1
IDL> print,!version
{ mipseb IRIX unix 5.1.1 Jul 20 1998}
What version of IDL are you using?
> Just try to pass var=2 into a routine and print keyword_set(var). Hope,
> David will take notice of this in his article.
>
> Another marginal point about setting default values: I recently learned
> from someone's code (cannot remember whose), to use
> if (n_elements(var) ne 1) then var=default
> instead of
> if (n_elements(var) eq 0) then var=default
>
> The advantage being that you can prevent program crashes when someone
> passes a vector or array in what is supposed to be a scalar.
>
If I require a scalar I always do this...
IF n_elements(var) EQ 0 THEN var=default else var=var[0]
> And, finally: Use keyword_set when you want to make sure the value of a
> boolean flag is defined:
> flag = keyword_set(flag)
> Then, later in the code, it's just
> if (flag) then ...
> Or value = x+y*(flag), etc. which would crash otherwise.
>
This, in fact, is the only time I use keyword_set. On all other
keywords where values are being passed, I use n_elements() for input
and and arg_present for output.
> Regards,
> Martin.
>
> |||||||||||||||\\\\\\\\\\\\\-------------------///////////// //|||||||||||||||
> Martin Schultz, DEAS, Harvard University, 29 Oxford St., Pierce 109,
> Cambridge, MA 02138 phone (617) 496 8318 fax (617) 495 4551
> e-mail mgs@io.harvard.edu web http://www-as/people/staff/mgs/
--
William Daffer: 818-354-0161: vapuser@catspaw.jpl.nasa.gov
|
|
|