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 - iGlobalhas already been renamed as- iGlobalOK.
- Click OK. - No global variable - iGlobalis in your project. Now- iGlobalOKis 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. - iGlobalTestis listed instead of- iGlobal.
- Right-click in the view on the right. 
- Click Reject this Object and click OK. - The global variable - iGlobalis available in FB in your project. The variable- iGlobalTestis now specified in the objects where the previous variable occurred.- The error message in the message view reports that the - iGlobalidentifier 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_3with the scope of- VAR_INPUTand data type- INT. 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 in- FCTfor the entire project.- You see the change in the implementation part of - PLC_PRGin 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_INPUTvariables.
- Drag the invar1 : INT; entry to the position before the invar2. entry. - The - invar1declaration 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 example- iA. 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 - iAto- iCounter_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.