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

Re: Passing zero as a Parameter/ NOT KEYWORD_SET



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

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/