Variables declaration in common module (CommonModuleVariables)¶
| Type | Scope | Severity | Activated by default |
Minutes to fix |
Tags |
|---|---|---|---|---|---|
Error |
BSL |
Critical |
Yes |
5 |
standarddesign |
Description¶
In the 1C:Enterprise platform, common modules operate as libraries of procedures and functions and do not retain state between calls. Declaring module-level variables using the "Var" operator causes the variable to be reinitialized upon every call to the module.
To manage state, context must be passed explicitly via procedure and function parameters, data structures, or platform-specific caching mechanisms.
Examples¶
Incorrect code:
Var CacheValues; // Variable loses its value after the call ends
Function GetCache() Export
Return CacheValues;
EndFunction
Function GetCache(CurrentCache) Export // State is passed explicitly via parameters
Return CurrentCache;
EndFunction
Sources¶
Source: What variables, procedures and functions can be used in common modules?
Snippets¶
Diagnostic ignorance in code¶
// BSLLS:CommonModuleVariables-off
// BSLLS:CommonModuleVariables-on
Parameter for config¶
"CommonModuleVariables": false