Programming

This section of PLC programming conventions covers the following topics.

Also note the option to check programming conventions using TE1200 PLC Static Analysis.

General

Loops and conditions

  1. CASE statement only with enumeration instance [++]
  2. Declare limits of a loop as a constant [+]
  3. Handle all enumeration values of the enumeration variables in CASE statement [+]
  4. IF-ELSIF statements with ELSE branch [+]
  5. Order of IF conditions [+]

Error Codes

  1. Data type for error codes [++]
  2. Error information to functions and methods [+]
  3. Error information to function blocks [+]

Readability, maintainability

  1. No unused declarations/objects or useless code [++]
  2. No "magic numbers" [+]
  3. No multiple use of the same names [+]
  4. Same notation in declaration and implementation [+]
  5. Avoid empty statements (;) [+]
  6. Each assignment in separate line of code [+]

Libraries

Library development

  1. Library names [++]
  2. Revision control [++]
  3. Encapsulation of internal type definitions [+]
  4. Identifiers in libraries [+]

Use of libraries

  1. Use of libraries [++]
  2. Version check [++]
  3. Fix library versions [+]

DUTs

Implementation of DUTs

  1. Use attributes 'qualified_only' and 'strict' for enumeration [++]
  2. Initialize no value, only the first value or all values for enumeration [+]

Use of DUTs

  1. Assign enumeration variables to associated enumerators only [++]

POUs

Implementation of functions, methods, actions

  1. No "call by value" of large parameters in functions/methods [++]
  2. Do not declare large variables in functions/methods [++]
  3. Do not use actions [+]
  4. Use all parameters of a function/method internally [+]
  5. Assign return value of a function/method only in one place [+]
  6. Restrict access to methods as much as possible [+]
  7. Grouping of parameters as structure [+]

Use of functions, methods

  1. Evaluate returned error information of a POU [++]
  2. Use return value of a function/method [+]
  3. Do not call functions/methods within themselves [+]

Implementation of function blocks

  1. Ensure online change capability [++]
  2. Uniform interface with one-time asynchronous processing [+]
  3. Uniform interface with continuous asynchronous processing [+]
  4. Use all parameters of a function block internally [+]
  5. Grouping of parameters as structure [+]

Use of function blocks

  1. Do not declare function block instances as VAR PERSISTENT [++]
  2. Evaluate returned error information of a POU [++]
  3. Do not access local variables of a POU from outside [++]
  4. No direct assignment of objects [++]
  5. No temporary function block instances [+]

Variables

General

  1. Do not test floating point numbers for equality or inequality [++]
  2. Avoid unwanted results using explicit casting [+]

Variable encapsulation

  1. Declare unchanged variables as VAR CONSTANT [+]
  2. Do not shade global identifiers [+]
  3. Restrict ADS access [+]

Arrays

  1. Define array limits via constants [++]
  2. Array lower limit of 1 [+]

Pointers, references, interfaces

  1. Temporary existence of pointers/references/interfaces to temporarily existing objects [++]
  2. Reset pointer/references every cycle [++]
  3. Check pointers/references/interfaces before each use [++]
  4. Prefer references over pointers [++]

Allocated variables

  1. Do not use direct addressing [++]
  2. Avoid multiple write accesses to outputs [++]
  3. Avoid overlapping memory areas of address variables [+]

Global variables

  1. Use attribute 'qualified_only' for GVL [+]
  2. Use global variables wisely [+]

Strings

  1. Identifier for size and length specifications [++]
  2. Recommended default size [+]
  3. Transfer of large strings [+]
  4. Processing of large strings [+]

Runtime behavior

General

  1. Intercept division by zero. [++]

Dynamic memory

  1. Perform dynamic memory allocations carefully [++]

Multiple tasks

  1. Avoid concurrent accesses to memory areas [++]
  2. Avoid concurrent accesses to function blocks [++]
  3. Use global variables wisely [+]