By Leon Jenkins


When writing InDesign scripts using JavaScript, the statements you will use in your code will be many and varied; however, one element which you will definitely use is variables, named sections, of memory in which you can store various types of data as well as references to the, various objects within the InDesign hierarchy, such as documents, pages and text frames. Essentially, a variable is a name which represents a value, typically, some piece of useful, information: wherever you use the name, that particular piece of data is targeted. In one, context, a variable is like an abbreviation or acronym: "Whenever I say docNew, I mean, the new document that I have just created". However, the great thing about variables is, that the values and elements they represent can be constantly altered and manipulated: by your code, by user action or by the InDesign environment. It all depends on the nature of the values, information or object assigned to the variable.

Almost every script that you write will contain many variables, so the names that you assign to variables will have an impact on the clarity of the code you end up with. Naturally, there, are some restrictions on the names that you can use. You cannot use as a variable name a word which is part of the JavaScript syntax. This includes some fairly common words such as "if", "switch", "for" and "final". Although there is no such restriction on using InDesign object names as variable names, it is a bad idea, since it makes you code harder to decipher. (The InDesign sample scripts use a convention of giving variables which hold objects the same name as the object with the prefix "my": e.g. "myTextFrame".)

As per usual, you are not allowed to use spaces in variable names cannot contain any spaces or special characters. This helps to avoid ambiguity: for example, a hyphen could be mistaken for a minus sign or a forward slash for divide.

You cannot start a variable name with a number or a special character. The first character must be a letter or an underscore.

Also, don't forget that JavaScript is case-sensitive; so you should decide on a set of rules regarding how you will use case in your variable names.

Taking these restrictions into consideration, it is usually best to adopt a system of some sort when naming your variables. One technique widely used by programmers is the use of prefixes which indicate the nature of the variable, typically, the type of data it is meant to store.

A useful technique is to use a minimum of two words for each variable name and to separate them either with an underscore or by capitalizing the first letter of each, word.

Another good policy is to be consistent with your use of case: for example, always use lower or initial uppercase letters, and so forth. This way, you will not have to constantly scroll back to check the case you used for individual variables.




About the Author:



0 comments