This post was originally written for Iguana 5 so it contains version 5 screenshots, and may contain out of date references.
When you are writing scripts in the Iguana Translator, it is easy to refactor your code to organize it into functions.For example, suppose that your
main() function maps patient ID and name data to database tables:
Tip: Even if your function does not need a
return statement, adding one as the function’s last statement is useful. This is because an annotation is generated for the
return statement. You can examine this annotation to see if the function has behaved the way you expect it to.
It would be handy to refactor the code to move the statements that map patient data to their own function, which could be called
MapPatient(). This enables your script to reuse this function later in the script if needed.
To start the refactoring process, create the
See how the function definition is all on a single line. This is supported in Lua, and ensures that an error message such as the following does not appear on the screen:
main(), add a call to the
MapPatient() function that you just created:
MapPatient() now has an annotation adjacent to it, as it is now being called by the script while it is processing the incoming message.
The next step is to move the three statements that perform mapping into
The Iguana Translator now generates an error. Since
Out is defined as a local variable in
main(), it is not defined in
Note: It is usually a good idea to define variables as
local whenever possible. This decreases the chances of your script behaving in a way that you do not expect.
At this point, you need to replace
T. This is because the contents of
Out.patient were passed as an argument to
MapPatient(), these contents are assigned to the variable
Tip: In the Iguana Translator, using short variable names is best, as this leaves more room for annotations to be displayed. You do not need to create descriptive variable names, as the contents of a variable are always clearly indicated in the annotations that are adjacent to statements that reference the variable.
When you replace
T, you get this:
You now get a different error, warning you that the variable
Msg is not defined in
MapPatient(). Again, this is because
Msg is defined as a local variable in
main(). To correct this error, replace
The first mapping statement has now been successfully refactored: no errors are displayed for it. The final step is to make the changes to the other two statements that you made to the first one:
Your script code has now been successfully refactored.
main(), it is useful to add a
return statement as the last statement in the function definition:
This enables you to examine the adjacent annotation to determine whether the mapping was performed properly.