日本語 English

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

Style list
LanguageVariables/functionsClass namesConstantOfficial/standard conventions
JavaScript/TypeScriptCamelCasePascalCaseUPPER_CASEMDN / Standard
Pythonsnake_casePascalCaseUPPER_CASEPEP 8
JavacamelCasePascalCaseUPPER_CASEOracle Code Conventions
C#Variables: camelCase
Functions: PascalCase
PascalCasePascalCaseMicrosoft naming guidelines
C++snake_case or camelCasePascalCase or snake_caseUPPER_CASEDepends on the organization (Google, etc.)
GocamelCase or PascalCase(no classes)camelCase or PascalCaseEffective Go

Example variable names

General
VariablesUse
cntFOR loop counters, etc.
indexWhen specifying array element numbers, etc.
offsetSpecifying element numbers from an offset position in array data, etc.
requestFlag when a request is received
runExecuting, execution command, etc.
FileNameFile name
For motion
VariablesUse
startRequestStart request
stopRequestStop request
emergencyStopEmergency stop
swEmergencyStopEmergency stop button
isRunningRunning
currentPositionCurrent position
targetPositionTarget position
For communication
VariablesUse
connectionConnection request
connectedConnected
sendMessageSend message
sendDataSend data
responceDataReceived data
sndDatSend data (BYTE array)
rcvDatReceived data (BYTE array)
sndSizeSend size
rcvSizeReceive size

Variable names to avoid

Variable names to stop using
VariablesImprovementReason
flag1MotionStartFlagWhen you look later, you cannot tell what the flag is for
W0_00, D100Name by function, not physical address Names based on physical addresses (W or D) ruin the benefit of variable programming (automatic memory allocation).
input, output, errorEmergencyStopSw or E-stopEasily confused with system variables or instructions, causing compile errors and bugs.
Single letters such as f, d, tIs_FilledExcept for loop counters (i or j), single-letter global variable names make searching the program (cross reference) extremely difficult.

Related pages:

Comments