Re: Passing zero as a Parameter/ NOT KEYWORD_SET [message #16062 is a reply to message #16040] |
Wed, 30 June 1999 00:00   |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
Martin Schultz wrote:
> 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" !
> 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
>
Hallo Martin,
this is not the best way to set variables because
e.g
pro test,var1
if (n_elements(var1) ne 1) then var1=2
help,var1
end
test,var1 & test,var1 & help,var1
You see after executing test, var1 has a value.
Better is:
pro test,var1
if n_elements(var1) gt 0 then in_var1=var1 else in_var1=2
help,in_var1
end
R.Bauer
>
> The advantage being that you can prevent program crashes when someone
> passes a vector or array in what is supposed to be a scalar.
>
> 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.
>
> 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/
|
|
|