Skip to main content

SA0010: Arrays with only one component

Detects arrays with only one element

Justification: An array with one element can be replaced by a base-type variable. Access to this variable is considerably faster than access by index to the variable.

Exception: The length of an array is often determined by a constant and is a parameter for a program. Then the program can work with arrays of different lengths and does not have to be changed if the length is only 1. This kind of situation should be documented accordingly.

Importance: Low

Example 22. Example
PROGRAM PLC_PRG
VAR
    aoiEmpty : ARRAY [22..22] OF INT := [22];
    aorEmpty : ARRAY [2..2] OF REAL := [2.2];
    iVar : INT;
    rVAR : REAL;
END_VAR
iVar := aoiEmpty[22];
rVAR := aorEmpty[2];

Output in the Messages view:

  • sa_icon_message.png SA0010: Vacuous array element in variable 'aoiEmpty'

  • sa_icon_message.png SA0010: Vacuous array element in variable 'aorEmpty'