Difference between revisions of "Side effects"

m
m
Line 2: Line 2:
  
 
Analytica is unusual among computer languages in that ''it does not allow side effects,'' except in special cases where they aren’t harmful. This is a major reason why Analytica code is so much easier to write, read, and debug than standard procedural languages like C, C++, Basic, R, Python, and so on.  
 
Analytica is unusual among computer languages in that ''it does not allow side effects,'' except in special cases where they aren’t harmful. This is a major reason why Analytica code is so much easier to write, read, and debug than standard procedural languages like C, C++, Basic, R, Python, and so on.  
Analytica lets you [[assign|Assignment Operator :=]] to a local variable V inside a Definition, for example:
+
Analytica lets you [[Assignment Operator :=|assign]] to a local variable V inside a Definition, for example:
 
   Variable A := BEGIN
 
   Variable A := BEGIN
 
       Local v := 0;
 
       Local v := 0;

Revision as of 00:56, 19 December 2024

A variable A or a function F has a side effect when evaluating it also a changes the value of another variable B. Side effects are the bane of computer programmers because it means you can’t be sure of how B was computed without looking for some other variable or function that may change it, even they may be in a totally different part of the code. Computer scientists term this problem “referential opacity”. It makes code much harder to understand and debug.

Analytica is unusual among computer languages in that it does not allow side effects, except in special cases where they aren’t harmful. This is a major reason why Analytica code is so much easier to write, read, and debug than standard procedural languages like C, C++, Basic, R, Python, and so on. Analytica lets you assign to a local variable V inside a Definition, for example:

  Variable A := BEGIN
      Local v := 0;
      v := B*C^2
  END;

But that doesn’t cause referential opacity because the local variable is declared right there in the same definition.

The most common cause of side effects (and the consequent opacity) is an assignment to a global variable defined somewhere else. Analytica only allows assignments to global variables in these special cases:

  • You may assign to a global variable in an OnClick or OnChange attribute, which must be triggered by a user action, but not in a standard Definition. In this case, the assignment changes the definition of the variable to the assigned value or expression, so you can still see how it was computed.
  • You may assign to a global variable B in a variable or function A if B is specifically defined as ComputedBy(A). This is useful when it is convenient to compute B as well as A in the Definition of A. It doesn’t cause any referential opacity because you can see right in the Definition of B that it is computed by A.

See also

Comments


You are not allowed to post comments.