Debugging Tips

From Director Online Wiki
Jump to: navigation, search

The secret to efficient debugging is to write debugger-friendly code. Anyone who has had to debug someone else's code will understand why.

Here are some tips, in case someone (it might be you) ever needs to debug your code.

Comment your code

This is so important that a special page is reserved for describing how best to do it.

Don't nest functions

A line like the following is difficult to debug:

Munge(Transmogrify(1, Convert(#someValue)))

Use temporary variables to store the result of each function, then use that as input for the following function:

vTemp = Convert(#someValue)
vTemp = Transmogrify(1, vTemp)
Munge(vTemp)

This allows you to check quickly which function is producing an unexpected value.

Use meaningful words for handler and variable names

Those of you used to working in low level languages may prefer single character variable names. In a high-level language like Lingo or JavaScript, there is no speed benefit to be gained from this.

Use coding conventions

Lingo and Javascript Syntax are not case sensitive. However, a mixture of upper and lowercase can help your code read better. For example: mouseUp, exitFrame

Use a prefix of 'p' or 'g' for propery or global variables. For example:

property pThisIsMyProperty
global gThisIsMyGlobal

Note the use of upper and lowercase in the variable names.

Keep your lines of code short

Macromedia tells its behavior writers to use no more than 70 characters per line.

Use multiple line if ... then ... end if statements

In the following statement, it is difficult to know whether the Convert() handler is called:

if stringP(Munge(aValue)) then aValue = Convert(aValue)

Here, not only is it much clearer, but you can place a debug checkpoint on the line itself:

vTemp = Munge(aValue)
if stringP(vTemp) then
  aValue = Munge(vTemp)
end if

Place the name of the script on the first line of the script

When you are using inheritance an instance may call a handler in one of its ancestors. The value of me that appears in the variables pane of the debugger may not correspond to the name of the script that is currently showing. If you place the name of the script at the top of the code, you can quickly scroll up to check which script is actually being debugged.