Tip avoid using global variables

FORGET THE GLOBAL VARIABLES !

This means that you must no more use the Global keyword when declaring variables. We know that they were frequently used in V6 version. But we have obvious reasons to forget them:

1. This is unsafe

In service mode, the same process can execute in sequence the same code in two different contexts. By context we mean: user code and folder. Thus, the global variable used to manage contextual parameter values will no more be usable.

In service mode, we might use the same code in different context recursively. Global variables aren't compatible with such types of code.

2. This is not effective

In V6 version, 90% of the connection time are spent by the procedures that fill the global variables. For a given user, depending on the operation called in the session, less than 20% of the global variables value are really used. We can avoid wasting this time...

3. We have improved tools in order to replace them

The context can be directly accessed via a context instance. This means that we can even have two contexts and manage them in an effective way!

  • The context instance pointer is present on every class (this.ACTX). It gives you access to previous global variables such as the user code.
  • If you need to access to parameter values, use the 'ACTX.APARAM' child instance and the associated methods that go through lazy loading mode to optimize the access to parameter values.
  • If you need to access quickly to small parameter tables, you better create a buffer class so that you benefit from the caching of the parameter values through 'ACTX.ACACHE'.
  • If you need to send a set of parameters to another function, forget the global variables assigned before the call. You still can create a technical class to send a set of values, then you will only have to add the instance as a parameter to your function.

An example of this is the GUSRACS variable. In version 6, this global variable had a value equal to 2 when the current user has access to all the activity codes, and a value equal to 1 if this wasn't the case. Now in version 7, the corresponding context variable is ALLACS.

So the corresponding piece of version 6 code:
  If [V]GUSRACS=2
    Gosub ACTION_ALLOWED
  Endif
would have to be replaced in version 7 by:
  If this.ALLACS=[V]CST_AYES : # Use GACTX.ALLACS only if a current instance does not exist
    Gosub ACTION_ALLOWED
  Endif

Of course global variables still exist

But they are managed by the supervisor and can be used in some cases. Two examples are given:

  1. We now use global variables for constants in order to make the code easier to read. See the corresponding rule.
  2. When this is not available (that is you are neither in a Method nor in a Rule), there is a global default context available (GACTX).

In conclusion: use global variables only if you have no other solution.