Refactoring
In general, refactoring is a technique for improving the design of existing software code without changing the way it functions.
In CODESYS, refactoring provides functions for renaming objects and variables and updating referenced pins. You can display all occurrences of renamed objects and variables and then rename them all at once or individually. In , you can also configure where CODESYS will prompt you for refactoring.
Renaming global variables
Requirement: A project is open that includes at least a function block FB and a global variable list. The global variable list GVL is open in the editor and contains a variable declaration (example: iGlobal
). FB uses iGlobal
.
Select the global variable name
iGlobal
.In the context menu, click
.In the Rename dialog, specify a name in the New name input field (example:
iGlobalOK
) and click OK.The Refactoring dialog opens. In the device tree view on the left, the GVL and FB objects are highlighted in red and yellow. In the view on the right, FB in is open in its editor and
iGlobal
has already been renamed asiGlobalOK
.Click OK.
No global variable
iGlobal
is in your project. NowiGlobalOK
is everywhere.
Select the global variable name
iGlobal
.In the context menu, click
.In the Rename dialog, type a name in the New name input field, for example
iGlobalTest
, and click OK.The Refactoring dialog opens. In the device tree view on the left, the GVL and FB objects are highlighted in red and yellow. In the window on the right, the FB function block is open in its editor.
iGlobalTest
is listed instead ofiGlobal
.Right-click in the view on the right.
Click Reject this Object and click OK.
The global variable
iGlobal
is available in FB in your project. The variableiGlobalTest
is now specified in the objects where the previous variable occurred.The error message in the message view reports that the
iGlobal
identifier is not defined.
Adding and removing input variables
In the declaration part of blocks, you can add and delete input and output variables by using the refactoring commands. CODESYS performs updates at the occurrence locations and calling locations of the blocks. You can accept or reject these updates individually. The Refactoring dialog also opens for this purpose.
Requirement: The FCT
(function type) POU is open in the editor. The function already contains the input variables input1
, input2
, and inputx
. They are called in the PLC_PRG
and POU
programs.
Set the focus in the declaration part of the FCT function.
Click
.The default dialog opens for declaring variables.
Declare the variable
input_3
with the scope ofVAR_INPUT
and data typeINT
. Click OK to exit the dialog.The Refactoring dialog opens (see figure below). The affected locations are marked in yellow. (1)+(2)
In the upper right corner, select Add inputs with placeholder text from the list box. (3)
In the left side of the window, click one of the highlighted objects (for example,
PLC_PRG
). In the context menu, click Accept Whole Project to add the new variable at the new location of use inFCT
for the entire project.You see the change in the implementation part of
PLC_PRG
in the view on the right: The placeholder_REFACTOR_
appears at the location where the new variable was added.Click OK to close the Refactoring dialog.
Click
. Search the project for "_REFACTOR_" to check and edit the affected locations.Note: As an alternative, you can insert the new variable with another initialization value without working with a placeholder first. In this case, in Step 4 you select "Add inputs with the following value" and type the value in the field on the right side of the list box.

Example of a new variable with placeholder text in a CFC block

Note that you can also remove variables with refactoring.
Reordering variables in the declaration
In the declaration part of function blocks, you can change the order of declarations by refactoring. This is possible for declarations with scope VAR_INPUT
, VAR_OUTPUT
, or VAR_IN_OUT
.
Requirement: The declaration part of a POU is open and includes declarations, for example:
VAR_INPUT invar2 : INT; invar1 : INT; in : DUT; bvar : BOOL; invar3 : INT; END_VAR
Right-click in this declaration block to access the context menu.
Click
.The Reorder dialog opens with a list of
VAR_INPUT
variables.Drag the invar1 : INT; entry to the position before the invar2. entry.
The
invar1
declaration is now at the top position.Click OK to exit the dialog.
The Refactoring dialog opens. The affected locations are marked in yellow. (See figure above)
Click OK to accept the new order for the function block.
Changing a variable declaration and applying refactoring automatically
Refactoring helps you in the declaration when renaming variables (by means of "Auto Declare").
Requirement: Function block fb_A
.
Click
.The Options dialog opens.
Select the Refactoring category.
In Auto Declare, activate the On renaming variables and On adding or removing variables, or for changing the namespace options.
Double-click the function block
fb_A
.Select a variable in the declaration of
fb_A
, for exampleiA
. As an alternative, you can set the cursor before or in the variable.Specify Shift+F2).
(The Declare Variable dialog opens. The dialog includes the settings of
iA
.Change the name of
iA
toiCounter_A
.The option Changes by means of refactoring appears and is activated.
Click OK.
The dialog Refactoring Renaming from iA to iCounterA opens. All locations affected by the variable renaming are marked there.
Click OK to exit the dialog.
The changes are applied.