Skip to main content

Defining Naming Conventions

In the Static Analysis Settings dialog, on the Naming Conventions tab, define the naming conventions that you will follow in your code. You define prefixes for the identifiers of your POUs, variables, and data types depending on their scopes and qualifiers.

Then you can let the Static Analysis check compliance of the naming conventions.

For instructions, see: Checking for compliance to defined naming conventions

Defining prefixes

In the Static Analysis Settings dialog, on the Naming Conventions tab, define the naming conventions in the input field of the Prefix column.

  • Character literal

    Example:

    PRG_ in Prefixes for POUs, Prefixes for POU type, PROGRAM (102)

  • Multiple literals per entry which are separated by a comma

    Syntax: <prefix> ( , <next prefix> )*

    Example:

    PRG_, PRG in Prefixes for POUs, Prefixes for POU type, PROGRAM (102)

  • Regular expression RegEx which defines a set of prefixes

    Syntax: @ <expression>

    Examples:

    @x[a-dA-D] defines a prefix that begins with x, followed by exactly one character in the set a-dA-D

    @[A-Za-z][A-Za-z][A-Za-z][A-Za-z0-9] defines a prefix that consists of four characters: The first three characters are letters, and the fourth character can also be a number.

Defining prefixes for combinable data types

The following data types are based on standard data types, existing data types, or function blocks:

  • Pointer: POINTER TO <basic type>

  • Reference: REFERENCE TO <basic type>

  • Array: ARRAY[ <lower index bound> .. <upper index bound> ] OF <basic type>

These data types can be nested and mutually combined. When the Recursive prefixes for combinable data types option is selected, Static Analysis expects recursively combined prefixes for variables of such combinable data types. The prefixes follow the declaration and correspond to the naming conventions defined for the basic data types.

Example

The following naming conventions have been set:

  • i in Prefixes for variables, INT (26)

  • p in Prefixes for variables, POINTER (26)

  • ref in Prefixes for variables, REFERENCE (27)

  • struct in Prefixes for variables, Structure (32)

  • Recursive prefixes for combinable data types option: standard icon

The following code violates naming conventions and is difficult to read.

PROGRAM plc1
VAR
        var1 : ARRAY[100..110] OF ARRAY[30..50] OF ARRAY[6..7] OF BOOL;
        var2 : ARRAY[1..3] OF INT;
        var3 : REFERENCE TO INT;
        iVar4 : INT;
        iVar5 : INT;
        var6 : ARRAY[1..3] OF INT := [11, 22, 33];
        var7 : POINTER TO ARRAY[1..3] OF INT;
        var8 : REFERENCE TO INT;
        var9 : S_POLYGONLINE := (aiStart:=[1,1], aiPoint1:=[5,2], aiPoint2:=[7,3], aiPoint3:=[8,5], aiPoint4:=[5,7], aiEnd:=[1,1]);
        var0 : ARRAY[0..3] OF S_POLYGONLINE; // S_POLYGONLINE is a already defined structure
END_VAR

iVar4 := iVar4 + 1;
var8 REF= iVar4;
var1[100][30][6] := TRUE;
var9.aiPoint1[1] := 99;
var7 := ADR(var6);
var6[2] := 44;
iVar5 := var7^[2];
var0[0] := var9;

Static Analysis reports the following violations of naming conventions:

  • NC0102: Invalid name 'plc1': Expected prefix 'PRG_'

  • NC0014: Invalid variable name 'var0': Expected prefix 'astruct'

  • NC0014: Invalid variable name 'var1': Expected prefix 'aaax'

  • NC0014: Invalid variable name 'var2': Expected prefix 'ai'

  • NC0014: Invalid variable name 'var3': Expected prefix 'refi'

  • NC0014: Invalid variable name 'var6': Expected prefix 'ai'

  • NC0014: Invalid variable name 'var7': Expected prefix 'pai'

  • NC0014: Invalid variable name 'var8': Expected prefix 'refi'

  • NC0014: Invalid variable name 'var9': Expected prefix 'struct'

Defining prefixes for variables of an alias

You can define prefixes for variables of data type Alias as a literal with the placeholder {datatype}. Then Static Analysis expects the prefix of the base data type instead of the placeholder.

The Combine scope prefix with data type prefix option has no effect on the use of the prefix {datatype}.

Example

The following naming conventions have been set:

  • s in Prefixes for variables, Prefixes for types, STRING()19

  • A_{datatype} in Prefixes for variables, Alias (33)

Code

TYPE A_MESSAGE : STRING[50]; // base data type is STRING
END_TYPE

VAR
        A_sMessage_N1 : A_MESSAGE := 'Robot is running.';
        A_s_Message_N2 : A_MESSAGE := 'Robot has been finished.';
        As_Message_N3 : A_MESSAGE := 'Robot has been stopped.';
        AsMessage_N4 : A_MESSAGE := 'Error 3 has been occured.';
END_VAR

Static Analysis reports the following violation of naming convention:

  • NC0033: Invalid variable name 'As_Message_N3': Expected prefix 'A_s'

  • NC0033: Invalid variable name 'AsMessage_N4': Expected prefix 'A_s'

Defining prefixes for properties

You can define the prefix for POUs of data type PROPERTY as a literal with the placeholder {datatype}. Then Static Analysis expects the prefix of the return data type of the property instead of the placeholder.

The Combine scope prefix with data type prefix option has no effect on the use of the placeholder {datatype}.

Example

The following naming conventions have been set:

  • fb in Prefixes for variables, Prefixes for types, Function block instance

  • FB_ in Prefixes for POUs, Prefixes for POU type, FUNCTIONBLOCK (103)

  • prop_{datatype} in Prefixes for POUs, Prefixes for POU type, PROPERTY (107)

Code

FUNCTION_BLOCK blocka // Invalid function block name
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR
    iA : INT;
END_VAR
iA := iA + 1;

PROPERTY PUBLIC vara : INT // Invalid property name, return data type is INT
Get
        vara := iA;
Set
iA := vara;

PROGRAM plc1
VAR
        var10 : blocka; // Invalid variable name of typ function block
        iVar11: INT;
END_VAR

var10();
IF var10.vara > 500 THEN
        var10.vara := 0;
END_IF
iVar11 := var10.vara;

Static Analysis reports the following violation of naming conventions:

  • NC0102: Invalid name 'plc1': Expected prefix 'PRG_'

  • NC0031: Invalid variable name 'var10': Expected prefix 'fb'

  • NC0103: Invalid variable name ''blocka': Expected prefix 'FB_'

  • NC0107: Invalid variable name 'blocka.vara': Expected prefix 'prop_i'

Code according to naming conventions

FUNCTION_BLOCK FB_A
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR
    iA : INT;
END_VAR
iA := iA + 1;

PROPERTY PUBLIC prop_iA : INT
Get
        prop_iA := iA;
Set
        iA := prop_iA;

PROGRAM PRG_PLC1
VAR
        fbA : FB_A;
        iVar11: INT;
END_VAR

fbA();
IF fbA.prop_iA > 500 THEN
        fbA.prop_iA := 0;
END_IF
iVar11 := fbA.prop_iA;

Defining prefixes for structures

You can define a general prefix for structures in the project in Prefixes for DUTs, Structure (151). You can also give a structure a special prefix. The special prefix then overrides the general prefix.

The special prefix is defined in the data type declaration of the structure with the pragma {attribute 'nameprefix' := <special prefix> '}. Then start all variables of this structure with this prefix <special prefix>.

Example

The following naming conventions have been set:

  • struct in Prefixes for variables, Structure (32)

  • S_ in Prefixes for DUTs, Structure (151)

  • Recursive prefixes for combinable data types: standard icon

Code

TYPE S_POLYGONLINE :
STRUCT
        aiStart : ARRAY[1..2] OF INT := [-99, -99];
        aiPoint1 : ARRAY[1..2] OF INT;
        aiPoint2 : ARRAY[1..2] OF INT;
        aiPoint3 : ARRAY[1..2] OF INT;
        aiPoint4 : ARRAY[1..2] OF INT;
        aiEnd : ARRAY[1..2] OF INT := [99, 99];
END_STRUCT
END_TYPE

{attribute 'nameprefix' := 'penta'} // Pragma to define a special prefix
TYPE S_PENTA EXTENDS S_POLYGONLINE :
STRUCT
        aiPoint5 : ARRAY[1..2] OF INT;
END_STRUCT
END_TYPE

PROGRAM PRG_Compute
VAR
        structPolygon : S_POLYGONLINE := (aiStart:=[1,1], aiPoint1:=[5,2], aiPoint2:=[7,3], aiPoint3:=[8,5], aiPoint4:=[5,7], aiEnd:=[1,1]);
        structPentagon : S_PENTAGON := (aiStart:=[0,0], aiPoint1:=[1,1], aiPoint2:=[2,2], aiPoint3:=[3,3], aiPoint4:=[4,4], aiPoint5:=[5,5]);
        structLine0 : S_PENTA;
        pentaLine1 : S_PENTA;
        iXPoint: INT;
        aPoint0: ARRAY [1..2] OF INT;
        aPoint1: ARRAY [1..2] OF INT;
END_VAR

iXPoint := structPolygon.aiPoint1[1];
aPoint0 := structLine0.aiEnd;
aiPoint1 := pentaLine1.aiEnd;

Static Analysis reports the following violation of naming conventions:

  • Invalid variable name 'structLine0': Expected prefix 'penta'

Providing other CODESYS Static Analysis projects with naming conventions

Save your naming conventions in a CSA file. You can load and use this file in other CODESYS Static Analysis projects.

Procedure. Saving to a file
  1. Click Build → Static Analysis → Settings.

  2. Click the Save button.

    A file selection dialog opens. There you can store the naming conventions in a file under any name with the file extension CSA.

Procedure. Loading a file with naming conventions
  1. Click Build → Static Analysis → Settings.

  2. Click the Load button.

    The file selection dialog opens. Select one of the CSA files displayed below.