Tim Dietrich

Custom Software Developer

Home Services Portfolio Blog About Contact Newsletter

LiveCode: Variable Checking / Explicit Variables

LiveCode is very flexible when it comes to using variables. It does not require that you explicitly declare variables before using them. That flexibility certainly makes developing scripts a little faster. If you need a new variable, just reference it. For example, in the code below, I’m using a variable named “testVar.”
on mouseUp
   put "Test!" into testVar
   answer testVar
end mouseUp
Notice that I didn’t have to declare the variable. I simply referenced it, and LiveCode ran with it. Easy and convenient. However, that convenience can often lead to trouble, especially when you are working with longer and more complicated scripts. All it takes is misspelling a single variable’s name, and you could end up having to spend a considerable amount of time trying to debug your code. For example, take a look at this code. It is supposed to count from 1 to 3, and display each number in a dialog box.
on mouseUp
   put 1 into x
   repeat while x <= 3
	  answer x
	  put x + 1 into y
   end repeat
end mouseUp
Do you see the problem? Instead of incrementing the value of x, we’re incrementing the value of a variable named y (“put x + 1 into y”). A simple, but potentially costly mistake. As a result, LiveCode would end up stuck in an endless loop, because the value of x would remain at 1. Admittedly, this is a very simple example. But again, imagine a much more complex, longer script. Finding that simple mistake could end up taking a considerable amount of time. To help prevent these types of mistakes, you can enable LiveCode’s “Variable Checking” option. This option requires you to explicitly declare any variables that are being used in your script. To enable the option, select Edit > Variable Checking when you have the LiveCode Script Editor open. Continuing with the example above, with Variable Checking enabled, if you attempted to apply the changes to the script, you would receive a parse error. For example:

To correct the error, you would need to declare the "x" variable. Here’s the modified script, with x declared as a local variable.
on mouseUp
   local x
   put 1 into x
   repeat while x <= 3
	  answer x
	  put x + 1 into y
   end repeat
end mouseUp
At this point, if you were to apply the changes to the script, you would receive a second error:

This error is informing you that yet another undeclared variable (“y”) is being referenced. In this case, we don’t want to declare the variable. Instead, we want to correct the code. So the corrected script would look like this:
on mouseUp
   local x
   put 1 into x
   repeat while x <= 3
	  answer x
	  put x + 1 into x
   end repeat
end mouseUp
One final note about Variable Checking. I mentioned that you can enable it via the Edit menu. You can also make this a default for the IDE. To do so, open LiveCode’s Preferences, select Script Editor, and check the Script Complication Mode option.

As you can see, while Variable Checking might slow you down a little, it does help you to write better, more bug-free code.