Identifier

Rules for identifier designation

Namenskonvention

Rules for identifiers of variables

  • An identifier must not contain spaces or special characters.
  • This means that CODESYS ignores uppercase/lowercase. VAR1 and var1 indicate the same variable.
  • CODESYS recognizes underscores. For example, A_BCD and AB_CD are treated as two different identifiers. Multiple consecutive underscores are not permitted.
  • The length of an identifier is unlimited.

Rules for multiple use of identifiers (namespaces)

  • You are permitted to use an identifier locally only one time.

  • An identifier is not permitted to be identical to a keyword (for example, variable type).

  • You are permitted to use an identifier globally several times. If a local variable has the same name as a global variable, then the local variable has priority within the POU.

  • A variable that is defined in a global variable list can have the same name as a variable defined in another GVL. CODESYS provides features that extend the standard for the namespace and scope of variables:

    • Global namespace operator: An instance path that begins with a dot always opens a global namespace. If there is a local variable (for example, ivar) that has the same name as a global variable, then you refer to the global variable as .ivar.

    • The name of a global variable list can define the namespace uniquely for the include variables. Therefore, you can declare variables with the same name in different global variables list and still uniquely reference by prepending the list name.

      For example, globlist1.ivar := globlist2.ivar; (* ivar from GVL globlist2 is copied to ivar in GVL globlist1 *).

    • Variables that are defined in the global variable list of a library included in the project can be referenced uniquely with the following syntax: <namespace library>.<GVL name>.<variable name>.

      For example, globlist1.ivar := lib1.globlist1.ivar (* ivar from GVL globlist1 in library lib1 is copied to ivar in GVL globlist1 *).

  • When inserting a library, you also use the library manager to define a namespace. In this way, you can make unique references to library blocks and library via <namespace library>.<block name\|variable name>. Please note that when libraries are nested, you must reference the namespaces of all libraries are in succession

    Example: If Lib1 is referenced by Lib0, then the block func in Lib1 is addressed via Lib0.Lib1.fun: ivar := Lib0.Lib1.fun(4, 5); (* return value from func is copied to variable ivar in the project *)

See also

Recommendations

We recommend that you apply the following rules in addition to the items that you have to consider specifically for variables declaration. By doing this, you will achieve the best possible harmonization when assigning names.

Recommendations for variable names

In all applications and libraries, you should name variables with Hungarian notation, when possible:

For each variable, find a short, meaningful, English description for the base name. The first letter of words in the base name is uppercase and all other letters are lowercase (for example, FileSize).

Append lowercase prefixes to the base name depending on the data type of the variables:

Data type Prefix Comment
BOOL x*  
  b reserved
BYTE by Bit string; not for arithmetic operations
WORD w Bit string; not for arithmetic operations
DWORD dw Bit string; not for arithmetic operations
LWORD lw Bit string; not for arithmetic operations
     
SINT si  
USINT usi  
INT i  
UINT ui  
DINT di  
UDINT udi  
LINT li  
ULINT uli  
     
REAL r  
LREAL lr  
     
STRING s  
WSTRING ws  
TIME tim  
LTIME ltim  
TIME_OF_DAY tod  
DATE_AND_TIME dt  
DATE date  
ENUM e  
POINTER p  
ARRAY a  

*: x is used intentionally as a prefix for Boolean variables to differentiate from BYTE and to accommodate IEC programmers.

Examples

bySubIndix: BYTE;

xFlag: BOOL;

udiCounter: UDINT;
Identifier for Description Example
Nested declarations The prefixes are appended in the order of declaration: pabyTelegramData: POINTER TO ARRAY [0..7] OF BYTE;
Function block instances and variables of user-defined data types Prefix: short identifier for the name of the FB or data type

cansdoReceivedTelegram: CAN_SDOTelegram;

TYPE CAN_SDOTelegram : (* prefix: sdo *)

STRUCT

wIndex: WORD;

bySubIndex:BYTE;

byLen:BYTE;

aby: ARRAY [0..3] OF BYTE;

END_STRUCT

END_TYPE

Local constants Prefix: c_ followed by the type prefix and variable name

VAR CONSTANT

c_uiSyncID: UINT := 16#80;

END_VAR

Global variables and global constants An additional prefix (g_ or gc_) is appended to the library prefix.

VAR_GLOBAL

CAN_g_iTest: INT;

END_VAR

VAR_GLOBAL CONSTANT

ᅠᅠCAN_gc_dwExample: DWORD;

END_VAR

Recommendations for variable names CODESYS V3.x libraries

Identifier for Description Example
Variable names Corresponds to the description for variable names, with the exception that global variables and constants do not require library prefixes because the namespace replaces the function.

g_iTest: INT; (* declaration *)

CAN.g_iTest (usage, call in the program

Recommendations for identifiers for user-defined data types (DUT)

Identifier for Description Example
Structures Library prefix followed by an underscore and a short, informative description of the structure. The associated prefix for created variables of this structure should follow the colon as a comment.

TYPE CAN_SDOTelegram : (* prefix: sdo *)

STRUCT

wIndex:WORD;

bySubIndex:BYTE;

byLen:BYTE;

abyData: ARRAY [0..3] OF BYTE;

END_STRUCT

END_TYPE

Enumerations

Library prefix followed by an underscore and the identifier in uppercase.

Please note: In past CODESYS versions, ENUM values > 16#7FFF caused errors because they were not automatically converted to INT. Therefore, ENUMs should always be defined with correct INT values.

TYPE CAL_Day :(

CAL_MONDAY,

CAL_TUESDAY,

CAL_WEDNESDAY,

CAL_THURSDAY,

CAL_FRIDAY,

ᅠCAL_SATURDAY,

CAL_SUNDAY);

Declaration:

eToday: CAL_Day;

Recommendations for identifiers for user-defined data types (DUT) in CODESYS V3 libraries

Identifier for Description Example
DUT names in CODESYS V3 libraries The namespace replaces the need for the library prefix. Therefore, it is omitted. Enumeration values are also defined without a library prefix.

TYPE Day :(

MONDAY,

TUESDAY,

WEDNESDAY,

THURSDAY,

FRIDAY,

SATURDAY,

SUNDAY);

Declaration:

eToday: CAL.Day;

Use in the application

IF eToday = CAL.Day.MONDAY THEN

Recommendations for identifiers for POUs, functions, function blocks, programs

Identifier for Description Example
POUs: Functions, function blocks, programs

Library prefix followed by an underscore and a short, informative POU name. Like for variables, the first letter of each word is uppercase and all other letters are lowercase. We recommend that you compose the POU name from a verb and a noun.

For function blocks, the associated prefix for created instances should follow the name as a comment.

FUNCTION_BLOCK CAN_SendTelegram (* prefix: canst *)
Actions

Only actions that the block itself calls, beginning with prv_.

Otherwise, actions do not contain prefixes.

 

Recommendations for identifiers for POUs in CODESYS V3 libraries

Identifier for Description Example
POUs The namespace replaces the need for the library prefix. Therefore, it is omitted. FUNCTION_BLOCK CAN_SendTelegram (* prefix: canst *)
Methods

Only methods that the block itself calls, beginning with prv_.

Otherwise, methods do not contain prefixes.

 
Interfaces Beginning with I ICANDevice

Recommendations for identifiers for visualizations

Hint

Please note that a visualization is not named the same as another block in the project because this may cause problems when changing visualizations.

See also