Variable encapsulation
Topics:
Declare unchanged variables as VAR CONSTANT
Variables that are not modified should be declared as VAR CONSTANT.
Static Analysis:
Check with the help of Static Analysis rule:
Negative sample:
PROGRAM Sample_neg
VAR
fTest : REAL := 1E-20; // Test value
cFloatEpsilon : REAL := 1E-12; // NON COMPLIANT: cFloatEpsilon will not be changed, but is not declared as VAR CONSTANT
END_VAR
--------------------------------------------------------------------
IF (fTest > (0 - cFloatEpsilon)) AND (fTest < (0 + cFloatEpsilon)) THEN
F_DoSomethingUsefulHere(); // it's just a sample
END_IF
Positive sample:
PROGRAM Sample_pos
VAR
fTest : REAL := 1E-20; // Test value
END_VAR
VAR CONSTANT
cFloatEpsilon : REAL := 1E-12; // COMPLIANT: cFloatEpsilon will not be changed and is declared as VAR CONSTANT
END_VAR
--------------------------------------------------------------------
IF (fTest > (0 - cFloatEpsilon)) AND (fTest < (0 + cFloatEpsilon)) THEN
F_DoSomethingUsefulHere(); // it's just a sample
END_IF
Do not shade global identifiers
Identifiers in an inner relationship should not "shade" more global identifiers. Identifiers in an inner relationship include variables that are instantiated in a method. In this case, more global identifiers are variables of the corresponding function block, for example.
Static Analysis:
Verify using the following Static Analysis rules:
The rule SA0027 is also included in the license-free variant Static Analysis Light.
General program elements for the following samples:
FUNCTION_BLOCK FB_Sample
VAR
sDescription : STRING; // STRING for POU description
END_VAR
--------------------------------------------------------------------
sDescription := 'This is a function block';
Negative sample:
METHOD PUBLIC nccl_Test : STRING
VAR
sDescription : STRING; // NON COMPLIANT: sDescription is already defined in method’s outer scope FB_Sample
END_VAR
--------------------------------------------------------------------
sDescription := 'This is a method';
nccl_Test := sDescription;
Positive sample:
METHOD PUBLIC nccl_Test : STRING
VAR
sMethodDescription : STRING; // COMPLIANT
END_VAR
--------------------------------------------------------------------
sMethodDescription := 'This is a method';
nccl_Test := sMethodDescription;
Restrict ADS access
If required, access to variables can be restricted so that there is no visibility via ADS. Such variables are then not usable for HMIs. For this purpose, the attribute 'TcNoSymbol' can be used.