The long answer on when to use a function vs command

Andre Garzia soapdog at mac.com
Wed Mar 15 00:29:44 EST 2006


Also, one important thing to notice is that commands (I am the only  
one that call them handlers?) do travel the message path so you can  
use all the message path 'manipulators' to work on them. For example  
for a simple timeout routine:

on timeout
    answer "Timeout!!!!"
end timeout

on doSomething
   local tTimeoutMsgID

  send timeout to me in 15 seconds
  put the result into tTimeoutMsgID -- Stores the message ID for the  
send in time operation
  doSomethingThatWillTakeTime
  cancel tTimeoutMsgID
end doSomething

So if the doSomethingThatWillTakeTime takes more than 15 seconds the  
timeout message will trigger, if not, the cancel operation will  
cancel the pendingMessage. This behaviour is much more complicate to  
achieve using functions for you can't send a function in time. My  
*style* is

	Cases when I use Commands/Handlers: When the code should be used  
with a 'send in time' opetation. When the command should be  
considered an Event, as in UI Events, Server transaction events, game  
workflow events. When I want OOP-style inheritance, like messages  
travelling up the message path from object->card->stack.

	Cases when I use functions: all the quick routines that simply are  
supposed to receive arguments and output results. All the repetitive  
tasks, if I identiy a piece of code that is used in two or more  
handlers, I make that piece a function and remove it from the handler.

I tend to think like Commands own the code and functions are handy  
things that Commands use. When I think a program, I think which  
events will happen, I then code them as Commands/Messages/Handlers  
then I make all the functions needed by those handlers. Thats how I  
think, which, I think is not how everyone thinks...

Cheers
andre




On Mar 15, 2006, at 1:46 AM, Ken Ray wrote:

> On 3/14/06 10:31 PM, "Thomas McGrath III" <3mcgrath at adelphia.net>  
> wrote:
>
>> Hello listeroos,
>>
>> Can someone give the long answer on when is it best to use a function
>> versus a command? I have a lot to write of both commands and
>> functions and have been overly confusing myself. Please give at least
>> two examples each with parameters and how to call/use them.
>
> In general, you use functions when you want to return some value,  
> and you
> use commands to "execute" things. However, both constructs allow  
> you to do
> both, so it's really a matter of preference.
>
> An example of a typical function:
>
> on mouseUp
>   put addEmUp(5,10,20) into field 1
> end mouseUp
>
> function addEmUp pNum1,pNum2,pNum3
>   put pNum1+pNum2+pNum3 into tTotal
>   return tTotal
> end addEmUp
>
> and a typical command:
>
> on mouseUp
>   NotifyUser "We're done!"
> end mouseUp
>
> on NotifyUser pMsg
>   put "Just wanted to tell you:" && pMsg into tText
>   answer tText
> end NotifyUser
>
> However you could (if you wanted to), reverse them:
>
> A function as a command:
>
> on mouseUp
>   addEmUp 5,10,20
>   put the result into field 1
> end mouseUp
>
> on addEmUp pNum1,pNum2,pNum3
>   put pNum1+pNum2+pNum3 into tTotal
>   return tTotal
> end addEmUp
>
> A command as a function:
>
> on mouseUp
>   get NotifyUser("We're done!")
> end mouseUp
>
> function NotifyUser pMsg
>   put "Just wanted to tell you:" && pMsg into tText
>   answer tText
> end NotifyUser
>
> Your call as to how to use them... but in general, a function  
> returns a
> value and a command doesn't.
>
> HTH,
>
> Ken Ray
> Sons of Thunder Software
> Web site: http://www.sonsothunder.com/
> Email: kray at sonsothunder.com
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your  
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution




More information about the use-livecode mailing list