elegant code tip

Rob Cozens rcozens at pon.net
Tue Dec 31 11:00:01 EST 2002


Hi Doug,

>to easily allow a boolean argument to be omitted, study this example:
>
>function searchMyList myList,toFind,matchWholeFlag
>   -- allow matchWholeFlag to be omitted (false)
>   set the wholeMatches to (matchWholeFlag = true)
>   return lineOffset(toFind,myList)
>end function

If you want it to fail when matchWholeFlag is empty, substitute:

"if matchWholeFlag then set wholeMatches to true else set 
wholdMatches to false"

   (should give "expected true or false" error)

If you want something more graceful, add

"if matchWholeFlag is empty then
    answer "Missing parameter"
    exit searchMyList
end if"

or (set a default)

"if matchWholeFlag is empty then put true into matchWholeFlag"

If you're just pointing out that appropriately-written handlers can 
deal with missing parameters, absolutely.  Many Serendipity Library 
handlers have one or more parameters that are never passed unless the 
caller wishes to override the default.  For example, the calling 
syntax for justifyString is:

	function justifyString theString,theLength,whichWay,padChar

whichWay defaults to "right" if empty; padChar defaults to space; so 
if the string is to be right-justified and the pad character is 
space, only the first two arguments must be passed.

The getSDBRecord command has six arguments.  Only the first argument 
is required.
-- 

Rob Cozens
CCW, Serendipity Software Company
http://www.oenolog.com/who.htm

"And I, which was two fooles, do so grow three;
Who are a little wise, the best fooles bee."

from "The Triple Foole" by John Donne (1572-1631)



More information about the use-livecode mailing list