[REBOL] [style] Feeling REBOLish (was: Notes on seeding 'random/seed)
From: greggirwin:mindspring at: 21-Mar-2004 11:44
Hi Alan,
First, thanks for all your detective work on RANDOM! Wow!
I did a few quick hacks against your function (as I added to my
storehouse of handy things :), and then I thought maybe it would be
useful to share my thoughts with the list.
Please don't take these comments as criticism, just thoughts about
style and how to make code more "REBOLish". Everyone, please feel free
to comment on my thoughts.
ap> randomize: func [
ap> {Reseed the random number generator using 'now/precise, or enter a date! time! integer!
argument.}
ap> seed [any-type!]
ap> ][
ap> random/seed either value? 'seed [seed][to-integer checksum/secure form now/precise]
ap> ]
If you only want to allow date!, time!, and integer!, use that in the
spec itself, rather than just as a comment and a spec of any-type!.
seed [date! time! integer!]
Don't put implementation details ("using 'now/precise") in a function
spec comment unless they are important for a user to know about. That
is, keep the main comment as concise as possible.
{Reseed the random number generator.}
If you want to allow any-type! to be used, just feed the seed value
you get through the same filter as you do for now/precise. (note that
things like function! values still aren't going to work here :)
to-integer checksum/secure form seed
Rather than using a fixed argument, and checking to see if it has a
value, use a refinement that takes a parameter.
/with seed
I greatly prefer explicitly optional arguments (the refinement
approach) for readability and maintenance reasons.
ANY can be used as a replacement for EITHER. It's a subjective call,
so I often write a piece of code both ways and then pick the one that
seems clearer to me. ANY can be faster, but I don't like to give up
clarity unless the speed gains are significant (and important).
With all that said, here's my version of your function:
randomize: func [
"Reseed the random number generator."
/with seed
][
random/seed to-integer checksum/secure form any [seed now/precise]
]
Thanks again for your post!
-- Gregg