Scope: managed applications, mobile applications, and ordinary applications.
1. In most cases, use more suitable development tools supported by 1C:Enterprise instead of module variables. Since it is hard to control the visibility (usage) area of these variables, they often become sources of poorly reproducible errors.
Below you can see some examples of incorrect use and exceptions to the rule. For recommendations as to formatting variables in the module code, see Module structure.
2. Examples of unreasonable use of variables in modules of objects (catalogs, documents, record sets, data processors, reports, and other).
2.1. To transfer parameters between event subscription handlers and to object module event handlers from the external code, it is recommended that you use the AdditionalProperties object property. Incorrect:
Var FilesConversion Export;
Procedure BeforeWrite(Cancel)
If FilesConversion Then
...
EndProcedure
// calling code
FileObject.FilesConversion = True;
FileObject.Write();
Correct:
Procedure BeforeWrite(Cancel)
If AdditionalProperties.Property("FilesConversion") Then
...
EndProcedure
// calling code
FileObject.AdditionalProperties.Insert("FilesConversion", True);
FileObject.Write();
To transfer internal parameters between object module event handlers, it is advisable that you use non-export object module variables that cannot be accessed from an external code.
For example:
Var PreviousCompanyValue; // the Company attribute value before the object is written to the base
Procedure BeforeWrite(Cancel)
PreviousCompanyValue = ...; // generating a query to get a value before the object is written to the base
EndProcedure
Procedure OnWrite(Cancel)
If PreviousAttributeValue <> Company Then
// processing the attribute value change upon writing
...
EndIf;
EndProcedure
2.2. To process return codes (errors) in the module logic, it is recommended that you use string constants.
Incorrect:
Var NoErrors,
FillCheckProcessing_Error, // occurs if filling check processing returned a failure
ObjectWrite_Error, // occurs if an exception was thrown upon writing an object
ObjectLock_Error, // occurs while trying to lock an object
Procedure ExecuteRecalculation()
...
Result = ProcessDocuments(...);
If Result = ObjectWrite_Error Then
...
ElseIf Result = ObjectLock_Error Then
...
ElseIf ...
EndProcedure
...
////////////////////////////////////////////////////////////////////////////////
// MODULE INITIALIZATION
NoErrors = 1;
FillCheckProcessing_Error = 2;
ObjectWrite_Error = 3;
ObjectLock_Error = 4;
Correct example:
Procedure ExecuteRecalculation()
...
Result = ProcessDocuments(...);
If Result = "ObjectWriteError" Then
...
ElseIf Result = "ObjectLockError" Then
...
ElseIf ...
EndProcedure
2.3. To cache long calculated and frequently used values in procedures and functions, use modules with repeated use of return values upon the server call.
An exception to this rule is made when returning the calculation result in an export function is not allowed for security reasons. In this case, they are placed in the local module variable.
3. Examples of unreasonable use of variables in form modules.
3.1. To cache long calculated and frequently used values in procedures and functions, use modules with repeated use of return values.
Do not cache static and easily calculated information. In particular, do not cache values of predefined elements and enumerations in client variables of the form module. To get them on the client, use the PredefinedValue method.
3.2. To store and transfer intermediate calculation results between different form procedures and functions, use the following:
- Parameters of procedures and functions to transfer results by the call chain of subsidiary procedures and functions in the context of a single call.
- Form attributes if you need to store intermediate results between different calls from the client. (Note that values of server form module variables are not saved between calls from the client).
An exception to this rule is made when client form variables are used to store intermediate results in form idle handlers, external event handlers, and client event handlers of form items.
For example:
&AtClient
Var ImageSequenceNumber; // a numerator counter to name files upon scanning several images
...
&AtClient
Procedure ExternalEvent(Source, Event, Data)
If Source = "TWAIN" AND Event = "ImageAcquired" Then
If ImageSequenceNumber = Undefined Then
ImageSequenceNumber = 1;
EndIf;
ImageSequenceNumber = ImageSequenceNumber + 1;
// Saving a scanned document to a file with the ImageSequenceNumber number
// ...
EndIf;
EndProcedure
4. Use variables of a managed or an ordinary application to store client session parameters. For more information, see Using session parameters.