Naming Rules | How to Name Variables, Functions and FBs
The Trouble with Variable Names
Naming variables, POUs, functions and function blocks is a very troublesome task
I often spend several minutes deciding on a name, and after all that, while writing the program I end up needing a similar name and have to change it; it could not be more troublesome
How to Name Variables
Looking at how PC programmers name things, Hungarian notation stands out
There are two kinds, Systems Hungarian and Apps Hungarian, and in recent years names that express the meaning of the variable tend to be preferred over Systems Hungarian.
Any name is fine, but it seems best to look at lots of samples by those who came before and imitate their style of variable names
Systems Hungarian notation
The data type or similar is prefixed to the variable name
For example, intValue, strName, dPi
Apps Hungarian notation
It is a way to make bugs look like bugs, so aim to give variable names clear meanings
Examples: Row, Col, MathPi, FileWrite, CurrentPosition
For example, when calculating the area of a triangle as base × height / 2, if you write (2) where you meant (1), the mistake is easier to notice
Area=BaseLength*height/2 ...(1)
Area=BaseLength*Angle/2 ...(2)
Common rules
Singular and plural
Use plural for arrays and the like (e.g. users) and singular for single data (e.g. user).
Booleans
Prefix variables holding true/false with is, has, can, etc. (e.g. isActive, hasPermission).
Abbreviations
Widely recognized abbreviations such as sw for switch, sol for SolenoidValve and cnt for count are common, but avoid extreme abbreviations (such as u_n).
There is also a site like this (Japanese):
A summary of English words often used in programming
How to write variable names
Camel case
Like a camel's humps, CamelCase, RowCount and ColIndex join multiple words with the first letter of each word capitalized
These days, variables usually start with lowercase and classes with uppercase
Snake case
Like a snake, SNAKE_CASE, MAX_ROW and LIMIT_HIGH_VALUE are written in uppercase with words joined by underscores (_)
Many people seem to use this style for constants
Recommended styles by language in high-level languages
| Language | Variables/functions | Class names | Constant | Official/standard conventions |
|---|---|---|---|---|
| JavaScript/TypeScript | CamelCase | PascalCase | UPPER_CASE | MDN / Standard |
| Python | snake_case | PascalCase | UPPER_CASE | PEP 8 |
| Java | camelCase | PascalCase | UPPER_CASE | Oracle Code Conventions |
| C# | Variables: camelCase Functions: PascalCase | PascalCase | PascalCase | Microsoft naming guidelines |
| C++ | snake_case or camelCase | PascalCase or snake_case | UPPER_CASE | Depends on the organization (Google, etc.) |
| Go | camelCase or PascalCase | (no classes) | camelCase or PascalCase | Effective Go |
Example variable names
| Variables | Use |
|---|---|
| cnt | FOR loop counters, etc. |
| index | When specifying array element numbers, etc. |
| offset | Specifying element numbers from an offset position in array data, etc. |
| request | Flag when a request is received |
| run | Executing, execution command, etc. |
| FileName | File name |
| Variables | Use |
|---|---|
| startRequest | Start request |
| stopRequest | Stop request |
| emergencyStop | Emergency stop |
| swEmergencyStop | Emergency stop button |
| isRunning | Running |
| currentPosition | Current position |
| targetPosition | Target position |
| Variables | Use |
|---|---|
| connection | Connection request |
| connected | Connected |
| sendMessage | Send message |
| sendData | Send data |
| responceData | Received data |
| sndDat | Send data (BYTE array) |
| rcvDat | Received data (BYTE array) |
| sndSize | Send size |
| rcvSize | Receive size |
Variable names to avoid
| Variables | Improvement | Reason |
|---|---|---|
| flag1 | MotionStartFlag | When you look later, you cannot tell what the flag is for |
| W0_00, D100 | Name by function, not physical address | Names based on physical addresses (W or D) ruin the benefit of variable programming (automatic memory allocation). |
| input, output, error | EmergencyStopSw or E-stop | Easily confused with system variables or instructions, causing compile errors and bugs. |
| Single letters such as f, d, t | Is_Filled | Except for loop counters (i or j), single-letter global variable names make searching the program (cross reference) extremely difficult. |
