Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: About Standards and Best Practices

From: reboler:bol at: 3-Sep-2003 17:31

Hi Folks!, Thanks to all of you. Elan, Greg, Ashley and Andrew. These are real good tips that makes me change the way I was programming. Other things I think should be interesting is the benchmark of functions.. a) conditional loops.. 'repeat instead of 'foreach, 'forall instead of 'foreach.; b) parse or load/markup (if using parse is faster than 'load/markup. You know, things like that. ok. Thank you again. Best regards, -DJ ----- Original Message ----- From: "Elan" <[rebol--techscribe--com]> To: <[rebol-list--rebol--com]> Sent: Wednesday, September 03, 2003 1:25 PM Subject: [REBOL] Re: About Standards and Best Practices
> Hi. > > Another issue that comes to mind is name space and side effects. If a > function introduces a word that will be used within the context of that > function only, then that word should be declared local to the function > in order to avoid name clashes that may result in unexpected side effects: > > f: func [ /local word] [ > word: "function's local word." > ] > > This prevents errors that result from setting a global word to some value,
then evaluating a function that happens to set the same word locally to a different value, and then continuing to use the global word. This is especially critical if functions are reused either by their original author or by some unsuspecting consumer. Example:
> ;--- consumer's script > REBOL [] > > ;- globally declared word > erase-harddrive-completely?: NO > > ;- evaluating function from a downloaded library > > do %down-loaded-library ;- contains function list-directory > > this-dir-contents: list-directory > . > . > . > if erase-harddrive-completely? [ erase-harddrive-completely ] > > ;--- end user's script > > So, what's the problem? Imagine the function "borrowed" from the
downloaded library looks like this:
> ;--- function in downloaded library -- > list-directory: func [ /interactive ] [ > erase-harddrive-completely?: "No" > directory-listing: read %. > if interactive [ > display-directory-listing > erase-harddrive-completely?: ask "Should I erase the hard drive
completely?"
> ] > directory-listing > ];---- end libraray function ---- > > Note that the user's script sets erase-harddrive-completely? to the logic!
value No, whereas the function sets the word erase-harddrive-completely? to the string! "No". The string! "No", however, evaluates as a logic! True: