In q, as in most languages that allow mutable state, a “variable” should properly be called an “assignable”. (See Mathematical Refresher above for a discussion). Nevertheless, a variable in q is a name together with associated storage that holds whatever value has been most recently assigned to the variable. As a consolation, at least q does not misuse ‘=’ for assignment, as do many languages; in q ‘=’ actually means “test for equality.” Declaring a variable and assigning its value are done in a single step with the operator (:) which is called “amend” and is read “is assigned” or “gets.” Here is how to create and assign variable a with integer value 42.
When you entered this in your q session, you noted that nothing is echoed to the console. In order to see that a has indeed been assigned, simply enter the variable.
A variable name must start with an alphabetic character, which can be followed by alpha, numeric or underscore.
Naming Style Recommendations
- Choose a name long enough to make the purpose of the entity evident, but no longer. The purpose of a name is to communicate to a reader of the code at another time— perhaps even you. Long names may not make code easier to read. For example, ‘checkDisk’ is clearer than ‘cd’ or ‘chk’ but is no less clear than ‘checkDiskForFileConsistency’.
- Use verbs for function names; use nouns for data.
- Be consistent in your use of abbreviations. Be mindful that even “obvious” abbreviations may be opaque to readers whose native language is different than yours.
- Be consistent in your use of capitalization, such as initial caps, camel casing, etc. Pick a style and stick to it.
- Use contexts for name spacing functions.
- Do not use names such as ‘int’, ‘float’ or other words that have meaning in q. While not reserved, they carry special meaning when used as arguments for certain q operators.
- Accomplished q programmers avoid using the underscore character in q names. If you insist on using underscore in names, do not use it as the last character. Expressions involving the built-in (_) operator and names with underscore will be difficult to read.
Reprinted with the author's permission from: q for Mortals Version 3, An Introduction to Q Programming by Jeffry A. Borror.
©2015 Jeffry A. Borror/ q4m LLC