QforMortals3/Order of Evaluation

From Kx Wiki
Jump to: navigation, search

1.7 Order of Evaluation

The interpreter evaluates the above specification of b from right-to-left (more on this in Chapter 4). If it were verbose, the interpreter might say:

The integer 42 is assigned to a variable named a, then the result of the assignment, namely 42, is added to the integer 1, then this result is assigned to a variable named b

Because the interpreter always evaluates expressions right-to-left, programmers can safely read q expressions left-to-right,

The variable b gets the value of the integer 1 plus the value assigned to the variable a, which gets the integer 42

This is exactly as in mathematics where we would read f(g(x)) as “f of g of x” even though g is evaluated first and the result passed into f. We just dispense with the parentheses.

Recommendations on Assignment Style

  1. The ability to chain evaluation of expressions permits a single line of q code to perform the work of an entire verbose program. In general this is acceptable (even good) q style when not taken to the extreme with extremely long wrapped lines or nested sub expressions.
  2. Intra-line assignments, as above, can simplify code provided they are few and are referenced only within the line of creation.
  3. It is not bad form to make one assignment per line, provided you don’t end up with one operation per line.
  4. Wannabe q gods carry terseness to the extreme, which quickly leads to write-only code.

Prev: Assignment, Next: Data Types 101

Reprinted with the author's permission from: q for Mortals Version 3, An Introduction to Q Programming by Jeffry A. Borror.

The book is available on Amazon. In the United Kingdom, it is available at Amazon UK.

©2015 Jeffry A. Borror/ q4m LLC

Personal tools