I'm interested in learning the good ways to wrap commonly-used code into a callable routine in Revolution.
[Question #1]
Currently, I define custom message handlers and send a message to the object containing the handler where appropriate. I use custom messages rather than functions since functions must return a value in Revolution, and most of my routines don't return anything. Is this a good solution? I've heard that functions can execute faster than messages after an initial run since they become compiled (the technicalities of this statement might be flawed, as this is just from word-of-mouth, so please correct me or inform me), so think there might be a better way.
[Question #2]
When I use messages and would like to return a value, is it a good idea to place the "return value" into the it variable?
[Question #3]
If I define a function in a stack/card/object script, can I call it from another without a problem?
Any help would be great. I'm trying to improve my Revolution coding practices to "good" rather than "it works". Thanks.
Seeking advice: Functions vs. Messages vs. ?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Hi,
your questions are a matter of taste. I prefer to have functions for tasks that return a value and do not manipulate the User Interface in any way. I use handlers, when I need to change anything UI wise, for example dynamically setting the text of a menu button.
I find this to be easier to read. For returning something from a handler you execute:
If you return something from a handler then the value will be placed in the result.
Hope that gets you started.
Malte
your questions are a matter of taste. I prefer to have functions for tasks that return a value and do not manipulate the User Interface in any way. I use handlers, when I need to change anything UI wise, for example dynamically setting the text of a menu button.
I find this to be easier to read. For returning something from a handler you execute:
If you return something from a handler then the value will be placed in the result.
Code: Select all
on mouseUp
doSomething
answer the result
end mouseUp
on doSomething
--some stuff here
return "Victory is mine!"
end doSomething
Malte
Hi mokogobo,
It doesn't matter much whether you use commands or functions. Both are equally fast en both are compiled on the first run. In fact, the entire script is compiled, not just the function or command you are using. I believe compilation is done when a stack opens or when a script is set, which means that all code should run without delay.
Sometimes, it is easier to write
put function1(function2(var1)) into var3
instead of
command2 var1
put the result into var2
command1 var2
put the result into var3
As you can see, fuctions may decrease the number of lines tremendously.
It may also "feel" more logical --yes, this is a contradictio in terminis-- to use a function, if a script is designed to return a value. If a script is designed to return nothing, while you might want to have an error code available as an option, you can use a command.
put function1(myVar) into myVar2 --> no var for errors by default
command1 myVar
put the result into rslt --> you can get errors but you may ignore them
It is not a good idea to put anything into the it varable. The it variable can change while you don't notice it, especially in scripts that get a filename and read data from a file. Try to avoid explicit use of the it variable, if possible. Also, it is often easier to use descriptive variables instead of the it variable.
You can put a function or command handle in any script. If it is in the stack script, you can make it available with the start using command, or by inserting the script into front or back. If the handler is in the script of a control, you can still insert it into front or back.
Best,
Mark
It doesn't matter much whether you use commands or functions. Both are equally fast en both are compiled on the first run. In fact, the entire script is compiled, not just the function or command you are using. I believe compilation is done when a stack opens or when a script is set, which means that all code should run without delay.
Sometimes, it is easier to write
put function1(function2(var1)) into var3
instead of
command2 var1
put the result into var2
command1 var2
put the result into var3
As you can see, fuctions may decrease the number of lines tremendously.
It may also "feel" more logical --yes, this is a contradictio in terminis-- to use a function, if a script is designed to return a value. If a script is designed to return nothing, while you might want to have an error code available as an option, you can use a command.
put function1(myVar) into myVar2 --> no var for errors by default
command1 myVar
put the result into rslt --> you can get errors but you may ignore them
It is not a good idea to put anything into the it varable. The it variable can change while you don't notice it, especially in scripts that get a filename and read data from a file. Try to avoid explicit use of the it variable, if possible. Also, it is often easier to use descriptive variables instead of the it variable.
You can put a function or command handle in any script. If it is in the stack script, you can make it available with the start using command, or by inserting the script into front or back. If the handler is in the script of a control, you can still insert it into front or back.
Best,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode